Merge lp:~vds/desktopcouch/fix_attachment_reconnection into lp:desktopcouch

Proposed by Vincenzo Di Somma
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 180
Merged at revision: 181
Proposed branch: lp:~vds/desktopcouch/fix_attachment_reconnection
Merge into: lp:desktopcouch
Diff against target: 130 lines (+30/-17)
2 files modified
desktopcouch/records/server_base.py (+17/-8)
desktopcouch/records/tests/test_server.py (+13/-9)
To merge this branch: bzr merge lp:~vds/desktopcouch/fix_attachment_reconnection
Reviewer Review Type Date Requested Status
Manuel de la Peña (community) Approve
Chad Miller (community) Approve
Review via email: mp+37284@code.launchpad.net

Commit message

Fixes the problem with the reconnection of both the attachment and the db change.

Description of the change

Despite the name this branch fixes the problem with the reconnection of both the attachment and the db change.

To post a comment you must log in.
Revision history for this message
Chad Miller (cmiller) :
review: Approve
Revision history for this message
Manuel de la Peña (mandel) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'desktopcouch/records/server_base.py'
2--- desktopcouch/records/server_base.py 2010-09-30 19:43:12 +0000
3+++ desktopcouch/records/server_base.py 2010-10-01 16:41:11 +0000
4@@ -48,6 +48,19 @@
5 #DEFAULT_DESIGN_DOCUMENT = "design"
6 DEFAULT_DESIGN_DOCUMENT = None # each view in its own eponymous design doc.
7
8+
9+def get_changes(self, changes_since):
10+ """This method is used to monkey patch the database to provide a
11+ get_changes method"""
12+ # Can't use self._server.resource.get() directly because
13+ # it encodes "/".
14+ uri = couchdburi(
15+ self.resource.uri, "_changes",
16+ since=changes_since)
17+ resp, data = self.resource.http.request(uri, "GET", "", {})
18+ return resp, data
19+
20+
21 class FieldsConflict(Exception):
22 """Raised in case of an unrecoverable couchdb conflict."""
23
24@@ -191,6 +204,8 @@
25 raise NoSuchDatabase(self._database_name)
26 if self.db is None:
27 self.db = self._server[self._database_name]
28+ if not hasattr(self.db, 'get_changes'):
29+ setattr(self.db.__class__, 'get_changes', get_changes)
30 else:
31 # Monkey-patch the object the user already uses. Oook!
32 new_db = self._server[self._database_name]
33@@ -647,14 +662,8 @@
34 now = time()
35 call_count = 0
36 if not niceness or now > self._changes_last_used + niceness:
37-
38- # Can't use self._server.resource.get() directly because
39- # it encodes "/".
40- uri = couchdburi(
41- self._server.resource.uri, self.db.name, "_changes",
42- since=self._changes_since)
43- ## Assume server has not crashed and URI is the same. FIXME
44- resp, data = self._server.resource.http.request(uri, "GET", "", {})
45+ resp, data = self.with_reconnects(self.db.get_changes,
46+ self._changes_since)
47 if resp["status"] != '200':
48 raise IOError(
49 "HTTP response code %s.\n%s" % (resp["status"], data))
50
51=== modified file 'desktopcouch/records/tests/test_server.py'
52--- desktopcouch/records/tests/test_server.py 2010-09-30 19:43:12 +0000
53+++ desktopcouch/records/tests/test_server.py 2010-10-01 16:41:11 +0000
54@@ -21,6 +21,7 @@
55 import testtools
56 import os
57 import signal
58+import socket
59 import time
60
61 import desktopcouch.tests as test_environment
62@@ -382,7 +383,7 @@
63 # Ensure time is same.
64 self.assertEqual(saved_time, self.database._changes_last_used)
65
66- ###self.maybe_die() # should be able to survive couchdb death
67+ self.maybe_die() # should be able to survive couchdb death
68
69 # Next time we run, we get the same event again.
70 # Consume queued changes.
71@@ -446,7 +447,7 @@
72 constructed_record.attach(content, "nu/mbe/rs", "text/plain")
73 constructed_record.attach("string", "another document", "text/plain")
74
75- ###self.maybe_die() # should be able to survive couchdb death
76+ self.maybe_die() # should be able to survive couchdb death
77 constructed_record.attach("XXXXXXXXX", "never used", "text/plain")
78 constructed_record.detach("never used") # detach works before commit.
79
80@@ -459,7 +460,6 @@
81 self.assertRaises(KeyError, constructed_record.attach, content,
82 "another document", "text/x-rst")
83
84- ###self.maybe_die() # should be able to survive couchdb death
85 record_id = self.database.put_record(constructed_record)
86 retrieved_record = self.database.get_record(record_id)
87
88@@ -480,7 +480,7 @@
89 # get new
90 retrieved_record = self.database.get_record(record_id)
91
92- ###self.maybe_die() # should be able to survive couchdb death
93+ self.maybe_die() # should be able to survive couchdb death
94 # We can get a list of attachments.
95 self.assertEqual(set(retrieved_record.list_attachments()),
96 set(["nu/mbe/rs", "Document"]))
97@@ -503,7 +503,7 @@
98 self.assertEqual(out_data, content.getvalue())
99 self.assertEqual(out_content_type, "text/plain")
100
101- ###self.maybe_die() # should be able to survive couchdb death
102+ self.maybe_die() # should be able to survive couchdb death
103 # Asking for a named document that does not exist causes KeyError.
104 self.assertRaises(KeyError, retrieved_record.attachment_data,
105 "NoExist")
106@@ -605,8 +605,10 @@
107 return self.ctx
108
109 def maybe_die(self):
110- self.database.ensure_full_commit()
111- time.sleep(2)
112+ try:
113+ self.database.ensure_full_commit()
114+ except socket.error:
115+ print "CouchDB already dead..."
116 pid = find_pid(start_if_not_running=False, ctx=self.get_test_context())
117 if pid is None:
118 print "couchdb has already quit! That's unexpected."
119@@ -625,7 +627,9 @@
120 return self.ctx
121
122 def maybe_die(self):
123- self.database.ensure_full_commit()
124- time.sleep(2)
125+ try:
126+ self.database.ensure_full_commit()
127+ except socket.error:
128+ print "CouchDB already dead..."
129 stop_couchdb(ctx=self.get_test_context())
130 self.wait_until_server_dead()

Subscribers

People subscribed via source and target branches