mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2024-12-23 14:52:26 +00:00
Merge pull request #667 from tahoe-lafs/3265.reuse-storage-plugin-web-resource
reuse storage plugin web resource
This commit is contained in:
commit
0e82782b61
0
newsfragments/3265.minor
Normal file
0
newsfragments/3265.minor
Normal file
@ -16,6 +16,9 @@ from zope.interface import (
|
||||
from twisted.internet.defer import (
|
||||
succeed,
|
||||
)
|
||||
from twisted.web.resource import (
|
||||
Resource,
|
||||
)
|
||||
from twisted.web.static import (
|
||||
Data,
|
||||
)
|
||||
@ -78,10 +81,26 @@ class DummyStorage(object):
|
||||
rendered, as an aid to testing.
|
||||
"""
|
||||
items = configuration.items(self._client_section_name, [])
|
||||
return Data(
|
||||
resource = Data(
|
||||
dumps(dict(items)),
|
||||
b"text/json",
|
||||
)
|
||||
# Give it some dynamic stuff too.
|
||||
resource.putChild(b"counter", GetCounter())
|
||||
return resource
|
||||
|
||||
|
||||
class GetCounter(Resource, object):
|
||||
"""
|
||||
``GetCounter`` is a resource that returns a count of the number of times
|
||||
it has rendered a response to a GET request.
|
||||
|
||||
:ivar int value: The number of ``GET`` requests rendered so far.
|
||||
"""
|
||||
value = 0
|
||||
def render_GET(self, request):
|
||||
self.value += 1
|
||||
return dumps({"value": self.value})
|
||||
|
||||
|
||||
@implementer(RIDummy)
|
||||
|
@ -2,6 +2,7 @@ import hashlib
|
||||
from mock import Mock
|
||||
from json import (
|
||||
dumps,
|
||||
loads,
|
||||
)
|
||||
from fixtures import (
|
||||
TempDir,
|
||||
@ -25,6 +26,10 @@ from zope.interface.verify import (
|
||||
verifyObject,
|
||||
)
|
||||
|
||||
from hyperlink import (
|
||||
URL,
|
||||
)
|
||||
|
||||
from twisted.application.service import (
|
||||
Service,
|
||||
)
|
||||
@ -459,6 +464,32 @@ class StoragePluginWebPresence(AsyncTestCase):
|
||||
result = yield do_http(b"get", url)
|
||||
self.assertThat(result, Equals(dumps({b"web": b"1"})))
|
||||
|
||||
@inlineCallbacks
|
||||
def test_plugin_resource_persistent_across_requests(self):
|
||||
"""
|
||||
The plugin's resource is loaded and then saved and re-used for future
|
||||
requests.
|
||||
"""
|
||||
url = URL(
|
||||
scheme=u"http",
|
||||
host=u"127.0.0.1",
|
||||
port=self.port,
|
||||
path=(
|
||||
u"storage-plugins",
|
||||
self.storage_plugin.decode("utf-8"),
|
||||
u"counter",
|
||||
),
|
||||
).to_text().encode("utf-8")
|
||||
values = {
|
||||
loads((yield do_http(b"get", url)))[u"value"],
|
||||
loads((yield do_http(b"get", url)))[u"value"],
|
||||
}
|
||||
self.assertThat(
|
||||
values,
|
||||
# If the counter manages to go up then the state stuck around.
|
||||
Equals({1, 2}),
|
||||
)
|
||||
|
||||
|
||||
def make_broker(tub_maker=lambda h: Mock()):
|
||||
"""
|
||||
|
@ -29,6 +29,8 @@ class StoragePlugins(Resource, object):
|
||||
"""
|
||||
resources = self._client.get_client_storage_plugin_web_resources()
|
||||
try:
|
||||
return resources[segment]
|
||||
result = resources[segment]
|
||||
except KeyError:
|
||||
return NoResource()
|
||||
result = NoResource()
|
||||
self.putChild(segment, result)
|
||||
return result
|
||||
|
Loading…
Reference in New Issue
Block a user