Browse Source

client: Add must_close() decorator to nilmdb.Client, and fix tests

Test suite wasn't closing connections correctly.
tags/nilmdb-1.1
Jim Paris 11 years ago
parent
commit
b53ff31212
2 changed files with 18 additions and 0 deletions
  1. +1
    -0
      nilmdb/client/client.py
  2. +17
    -0
      tests/test_client.py

+ 1
- 0
nilmdb/client/client.py View File

@@ -13,6 +13,7 @@ def float_to_string(f):
"""Use repr to maintain full precision in the string output.""" """Use repr to maintain full precision in the string output."""
return repr(float(f)) return repr(float(f))


@nilmdb.utils.must_close()
class Client(object): class Client(object):
"""Main client interface to the Nilm database.""" """Main client interface to the Nilm database."""




+ 17
- 0
tests/test_client.py View File

@@ -50,20 +50,24 @@ class TestClient(object):
client = nilmdb.Client(url = "http://localhost:1/") client = nilmdb.Client(url = "http://localhost:1/")
with assert_raises(nilmdb.client.ServerError): with assert_raises(nilmdb.client.ServerError):
client.version() client.version()
client.close()


# Trigger same error with a PUT request # Trigger same error with a PUT request
client = nilmdb.Client(url = "http://localhost:1/") client = nilmdb.Client(url = "http://localhost:1/")
with assert_raises(nilmdb.client.ServerError): with assert_raises(nilmdb.client.ServerError):
client.version() client.version()
client.close()


# Then a fake URL on a real host # Then a fake URL on a real host
client = nilmdb.Client(url = "http://localhost:12380/fake/") client = nilmdb.Client(url = "http://localhost:12380/fake/")
with assert_raises(nilmdb.client.ClientError): with assert_raises(nilmdb.client.ClientError):
client.version() client.version()
client.close()


# Now a real URL with no http:// prefix # Now a real URL with no http:// prefix
client = nilmdb.Client(url = "localhost:12380") client = nilmdb.Client(url = "localhost:12380")
version = client.version() version = client.version()
client.close()


# Now use the real URL # Now use the real URL
client = nilmdb.Client(url = "http://localhost:12380/") client = nilmdb.Client(url = "http://localhost:12380/")
@@ -74,6 +78,7 @@ class TestClient(object):
# Bad URLs should give 404, not 500 # Bad URLs should give 404, not 500
with assert_raises(ClientError): with assert_raises(ClientError):
client.http.get("/stream/create") client.http.get("/stream/create")
client.close()


def test_client_2_createlist(self): def test_client_2_createlist(self):
# Basic stream tests, like those in test_nilmdb:test_stream # Basic stream tests, like those in test_nilmdb:test_stream
@@ -117,6 +122,7 @@ class TestClient(object):
client.stream_create("/newton/hello", "RawData") client.stream_create("/newton/hello", "RawData")
resource.setrlimit(resource.RLIMIT_FSIZE, limit) resource.setrlimit(resource.RLIMIT_FSIZE, limit)


client.close()


def test_client_3_metadata(self): def test_client_3_metadata(self):
client = nilmdb.Client(url = "http://localhost:12380/") client = nilmdb.Client(url = "http://localhost:12380/")
@@ -150,6 +156,7 @@ class TestClient(object):
client.stream_set_metadata("/newton/prep", [1,2,3]) client.stream_set_metadata("/newton/prep", [1,2,3])
with assert_raises(ClientError): with assert_raises(ClientError):
client.stream_update_metadata("/newton/prep", [1,2,3]) client.stream_update_metadata("/newton/prep", [1,2,3])
client.close()


def test_client_4_insert(self): def test_client_4_insert(self):
client = nilmdb.Client(url = "http://localhost:12380/") client = nilmdb.Client(url = "http://localhost:12380/")
@@ -226,6 +233,8 @@ class TestClient(object):
in_("400 Bad Request", str(e.exception)) in_("400 Bad Request", str(e.exception))
in_("verlap", str(e.exception)) in_("verlap", str(e.exception))


client.close()

def test_client_5_extractremove(self): def test_client_5_extractremove(self):
# Misc tests for extract and remove. Most of them are in test_cmdline. # Misc tests for extract and remove. Most of them are in test_cmdline.
client = nilmdb.Client(url = "http://localhost:12380/") client = nilmdb.Client(url = "http://localhost:12380/")
@@ -236,6 +245,8 @@ class TestClient(object):
with assert_raises(ClientError) as e: with assert_raises(ClientError) as e:
client.stream_remove("/newton/prep", 123, 120) client.stream_remove("/newton/prep", 123, 120)


client.close()

def test_client_6_generators(self): def test_client_6_generators(self):
# A lot of the client functionality is already tested by test_cmdline, # A lot of the client functionality is already tested by test_cmdline,
# but this gets a bit more coverage that cmdline misses. # but this gets a bit more coverage that cmdline misses.
@@ -285,6 +296,8 @@ class TestClient(object):
in_("404 Not Found", str(e.exception)) in_("404 Not Found", str(e.exception))
in_("No such stream", str(e.exception)) in_("No such stream", str(e.exception))


client.close()

def test_client_7_headers(self): def test_client_7_headers(self):
# Make sure that /stream/intervals and /stream/extract # Make sure that /stream/intervals and /stream/extract
# properly return streaming, chunked, text/plain response. # properly return streaming, chunked, text/plain response.
@@ -324,6 +337,8 @@ class TestClient(object):
"header in /stream/extract response:\n" + "header in /stream/extract response:\n" +
http._headers) http._headers)


client.close()

def test_client_8_unicode(self): def test_client_8_unicode(self):
# Basic Unicode tests # Basic Unicode tests
client = nilmdb.Client(url = "http://localhost:12380/") client = nilmdb.Client(url = "http://localhost:12380/")
@@ -361,6 +376,8 @@ class TestClient(object):
eq_(client.stream_get_metadata(raw[0], [ "alpha" ]), meta2) eq_(client.stream_get_metadata(raw[0], [ "alpha" ]), meta2)
eq_(client.stream_get_metadata(raw[0], [ "alpha", "β" ]), meta1) eq_(client.stream_get_metadata(raw[0], [ "alpha", "β" ]), meta1)


client.close()

def test_client_9_closing(self): def test_client_9_closing(self):
# Make sure we actually close sockets correctly. New # Make sure we actually close sockets correctly. New
# connections will block for a while if they're not, since the # connections will block for a while if they're not, since the


Loading…
Cancel
Save