Skip to content

Commit

Permalink
Merge pull request #789 from danielballan/memoryview-fix
Browse files Browse the repository at this point in the history
Fix regression in serialization of `memoryview`
  • Loading branch information
nmaytan authored Sep 19, 2024
2 parents dd8d8f1 + 1757bc7 commit 722d761
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Write the date in place of the "Unreleased" in the case a new version is release

### Fixed

- Follow-up fix to compatibility with Starlette v0.38.0
- Adapt to change in dask public API in dask 2024.9.0.

## v0.1.0b8 (2024-09-06)
Expand Down
7 changes: 7 additions & 0 deletions tiled/_tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..adapters.array import ArrayAdapter
from ..adapters.mapping import MapAdapter
from ..client import Context, from_context
from ..serialization.array import as_buffer
from ..server.app import build_app
from .utils import fail_with_status_code

Expand Down Expand Up @@ -163,3 +164,9 @@ def test_array_interface(context):
# smoke test
v.chunks
v.dims


@pytest.mark.parametrize("kind", list(array_cases))
def test_as_buffer(kind):
output = as_buffer(array_cases[kind], {})
assert len(output) == len(bytes(output))
35 changes: 24 additions & 11 deletions tiled/_tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import threading
import time

import pytest
import uvicorn
from fastapi import APIRouter
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR
Expand Down Expand Up @@ -39,12 +40,26 @@ def run_in_thread(self):
thread.join()


API_KEY = "secret"


@pytest.fixture
def server(tmpdir):
catalog = in_memory(writable_storage=tmpdir)
app = build_app(catalog, {"single_user_api_key": API_KEY})
app.include_router(router)
config = uvicorn.Config(app, port=0, loop="asyncio", log_config=LOGGING_CONFIG)
server = Server(config)
with server.run_in_thread() as url:
yield url


@router.get("/error")
def error():
1 / 0 # error!


def test_500_response():
def test_500_response(server):
"""
Test that unexpected server error returns 500 response.
Expand All @@ -56,14 +71,12 @@ def test_500_response():
This can happen when bugs are introduced in the middleware layer.
"""
API_KEY = "secret"
catalog = in_memory()
app = build_app(catalog, {"single_user_api_key": API_KEY})
app.include_router(router)
config = uvicorn.Config(app, port=0, loop="asyncio", log_config=LOGGING_CONFIG)
server = Server(config)

with server.run_in_thread() as url:
client = from_uri(url, api_key=API_KEY)
response = client.context.http_client.get(f"{url}/error")
client = from_uri(server, api_key=API_KEY)
response = client.context.http_client.get(f"{server}/error")
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR


def test_writing_integration(server):
client = from_uri(server, api_key=API_KEY)
x = client.write_array([1, 2, 3], key="array")
x[:]
2 changes: 1 addition & 1 deletion tiled/serialization/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def as_buffer(array, metadata):
# The memoryview path fails for datetime type (and possibly some others?)
# but it generally works for standard types like int, float, bool, str.
try:
return memoryview(numpy.ascontiguousarray(array))
return memoryview(numpy.ascontiguousarray(array)).cast("B")
except ValueError:
return numpy.asarray(array).tobytes()

Expand Down

0 comments on commit 722d761

Please sign in to comment.