Merge lp:~jameinel/u1db/win32-tweaks into lp:u1db

Proposed by John A Meinel
Status: Merged
Merged at revision: 107
Proposed branch: lp:~jameinel/u1db/win32-tweaks
Merge into: lp:u1db
Diff against target: 122 lines (+22/-3)
7 files modified
u1db/__init__.py (+4/-0)
u1db/backends/inmemory.py (+5/-0)
u1db/backends/sqlite_backend.py (+3/-0)
u1db/commandline/client.py (+1/-0)
u1db/tests/commandline/test_client.py (+4/-1)
u1db/tests/commandline/test_serve.py (+2/-2)
u1db/tests/test_backends.py (+3/-0)
To merge this branch: bzr merge lp:~jameinel/u1db/win32-tweaks
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+82156@code.launchpad.net

Description of the change

I logged into Windows and ran the test suite.

The tests generally seem to pass, but test suite cleanup fails because the sqlite file handle is still open. In general, trusting Python's gc to close file handles is pretty weak (which is why they invented context managers).

I think having an "I'm done with this Database object, please release its resources" is a reasonable thing to have.

The test suite now passes cleanly on Windows.

To post a comment you must log in.
lp:~jameinel/u1db/win32-tweaks updated
108. By John A Meinel

Don't nuke InMemoryDatabase.

We might call .close() from a commandline client, but we don't want to nuke the
data for the test case.

Revision history for this message
Samuele Pedroni (pedronis) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'u1db/__init__.py'
2--- u1db/__init__.py 2011-11-03 14:29:22 +0000
3+++ u1db/__init__.py 2011-11-14 15:01:26 +0000
4@@ -245,6 +245,10 @@
5 """
6 raise NotImplementedError(self.get_sync_generation)
7
8+ def close(self):
9+ """Release any resources associated with this database."""
10+ raise NotImplementedError(self.close)
11+
12
13 class SyncTarget(object):
14 """Functionality for using a Database as a synchronization target."""
15
16=== modified file 'u1db/backends/inmemory.py'
17--- u1db/backends/inmemory.py 2011-11-01 18:26:07 +0000
18+++ u1db/backends/inmemory.py 2011-11-14 15:01:26 +0000
19@@ -33,6 +33,11 @@
20 self._replica_uid = replica_uid
21 self._last_exchange_log = None
22
23+ def close(self):
24+ # This is a no-op, We don't want to free the data because one client
25+ # may be closing it, while another wants to inspect the results.
26+ pass
27+
28 def get_sync_generation(self, other_replica_uid):
29 return self._other_generations.get(other_replica_uid, 0)
30
31
32=== modified file 'u1db/backends/sqlite_backend.py'
33--- u1db/backends/sqlite_backend.py 2011-10-28 09:17:26 +0000
34+++ u1db/backends/sqlite_backend.py 2011-11-14 15:01:26 +0000
35@@ -68,6 +68,9 @@
36 """Release access to the underlying sqlite database."""
37 self._db_handle.close()
38
39+ def close(self):
40+ self._close_sqlite_handle()
41+
42 def _is_initialized(self, c):
43 """Check if this database has been initialized."""
44 c.execute("PRAGMA case_sensitive_like=ON")
45
46=== modified file 'u1db/commandline/client.py'
47--- u1db/commandline/client.py 2011-11-04 13:12:38 +0000
48+++ u1db/commandline/client.py 2011-11-14 15:01:26 +0000
49@@ -148,6 +148,7 @@
50 st = self._open_target(target)
51 syncer = sync.Synchronizer(source_db, st)
52 syncer.sync()
53+ source_db.close()
54
55 client_commands.register(CmdSync)
56
57
58=== modified file 'u1db/tests/commandline/test_client.py'
59--- u1db/tests/commandline/test_client.py 2011-11-04 13:12:38 +0000
60+++ u1db/tests/commandline/test_client.py 2011-11-14 15:01:26 +0000
61@@ -101,6 +101,7 @@
62 self.db_path = self.working_dir + '/test.db'
63 self.db = sqlite_backend.SQLitePartialExpandDatabase(self.db_path)
64 self.db._set_replica_uid('test')
65+ self.addCleanup(self.db.close)
66
67 def make_command(self, cls, stdin_content=''):
68 inf = cStringIO.StringIO(stdin_content)
69@@ -175,6 +176,7 @@
70 super(TestCmdSync, self).setUp()
71 self.db2_path = self.working_dir + '/test2.db'
72 self.db2 = sqlite_backend.SQLitePartialExpandDatabase(self.db2_path)
73+ self.addCleanup(self.db2.close)
74 self.db2._set_replica_uid('test2')
75 _, self.doc_rev = self.db.create_doc(tests.simple_doc,
76 doc_id='test-id')
77@@ -247,7 +249,7 @@
78 self.assertEqual(tests.simple_doc, doc)
79 self.assertFalse(has_conflicts)
80 self.assertEqual('id: test-id\nrev: %s\n' % (doc_rev,),
81- stderr)
82+ stderr.replace('\r\n', '\n'))
83
84 def test_get(self):
85 _, doc_rev = self.db.create_doc(tests.simple_doc, doc_id='test-id')
86@@ -277,6 +279,7 @@
87 _, doc_rev = self.db.create_doc(tests.simple_doc, doc_id='test-id')
88 self.db2_path = self.working_dir + '/test2.db'
89 self.db2 = sqlite_backend.SQLitePartialExpandDatabase(self.db2_path)
90+ self.addCleanup(self.db2.close)
91 ret, stdout, stderr = self.run_main(
92 ['sync', self.db_path, self.db2_path])
93 self.assertEqual(0, ret)
94
95=== modified file 'u1db/tests/commandline/test_serve.py'
96--- u1db/tests/commandline/test_serve.py 2011-11-04 13:05:54 +0000
97+++ u1db/tests/commandline/test_serve.py 2011-11-14 15:01:26 +0000
98@@ -67,8 +67,8 @@
99 host, port = s.getsockname()
100 s.close()
101 p = self.startU1DBServe(['--port', str(port)])
102- x = p.stdout.readline()
103- self.assertEqual('listening on port: %s\n' % (port,), x)
104+ x = p.stdout.readline().strip()
105+ self.assertEqual('listening on port: %s' % (port,), x)
106 s = socket.socket()
107 s.connect(('127.0.0.1', port))
108 self.addCleanup(s.close)
109
110=== modified file 'u1db/tests/test_backends.py'
111--- u1db/tests/test_backends.py 2011-11-02 14:12:14 +0000
112+++ u1db/tests/test_backends.py 2011-11-14 15:01:26 +0000
113@@ -26,6 +26,9 @@
114
115 class DatabaseTests(tests.DatabaseBaseTests):
116
117+ def test_close(self):
118+ self.db.close()
119+
120 def test_create_doc_allocating_doc_id(self):
121 doc_id, new_rev = self.db.create_doc(simple_doc)
122 self.assertNotEqual(None, doc_id)

Subscribers

People subscribed via source and target branches