Code review comment for lp:~al-maisan/landscape-client/obsolete-results-v

Revision history for this message
Muharem Hrnjadovic (al-maisan) wrote :

Hello Free,

thank you very much for these comments, I really appreciate it.
I have taken care of all of them, please see the attached incremental
diff.

On 04/30/2010 11:14 AM, Free Ekanayaka wrote:
> [9]
>
> + message_context_store,
>
> Please name this parameter "exchange_store", so it matches the name of the expected type (ExchangeStore) and it is shorter. Also, now that we have it, the ExchangeStore could be used to store also other types of information beside MessageContext's, so it makes sense to use a generic name for it.
>
> + self._store = message_context_store
>
> Similarly please name this attribute "self._exchange_store", to make it clear which store is referring to (there is another attribute called "self._message_store", so the naming will be consistent).
>
> [10]
>
> + else:
> + if "timestamp" not in message:
> + message["timestamp"] = int(self._reactor.time())
> + message_id = self._message_store.add(message)
> + if urgent:
> + self.schedule_exchange(urgent=True)
> + return message_id
>
> I think you can save this else/indentation level and simply write:
>
> + if "timestamp" not in message:
> + message["timestamp"] = int(self._reactor.time())
> + message_id = self._message_store.add(message)
> + if urgent:
> + self.schedule_exchange(urgent=True)
> + return message_id
>
> I personally like the pattern when you check a series of early-return conditions, before reaching the meat of a function/method, like
>
> if this:
> return something
> if that:
> return something_else
>
> do_the_actual_thing
>
> [11]
>
> + exchange_store = ExchangeStore(
> + os.path.join(config.data_path, "exchange.database"))
>
> Just to avoid hard-coding this value you could add a property to l.broker.config.BrokerConfiguration, like:
>
> @property
> def exchange_store_path(self):
> return os.path.join(self.data_path, "exchange.database"))
>
> and use it as
>
> + exchange_store = ExchangeStore(self.config.exchange_store_path)
>
> [12]
>
> + exchange_store = ExchangeStore(
> + os.path.join(test_case.config.data_path, "exchange.database"))
>
> Please add this object to the test_case attributes, so it can be used in tests:
>
> + test_case.exchange_store = ExchangeStore(
> + os.path.join(test_case.config.data_path, "exchange.database"))
>
> after that you can avoid creating it explicitly in all the tests.
>
> [13]
>
> + self.exchanger._store.get_message_context(message['operation-id']))
>
> Once you have self.exchange_store as test attribute (see [12]) you should use it instead of accessing this private attribute.
>
> Thanks!

Best regards

--
Muharem Hrnjadovic <email address hidden>
Public key id : B2BBFCFC
Key fingerprint : A5A3 CC67 2B87 D641 103F 5602 219F 6B60 B2BB FCFC

1=== modified file 'landscape/broker/config.py'
2--- landscape/broker/config.py 2010-01-06 15:11:24 +0000
3+++ landscape/broker/config.py 2010-04-30 10:39:07 +0000
4@@ -18,6 +18,10 @@
5 self._original_http_proxy = os.environ.get("http_proxy")
6 self._original_https_proxy = os.environ.get("https_proxy")
7
8+ @property
9+ def exchange_store_path(self):
10+ return os.path.join(self.data_path, "exchange.database")
11+
12 def make_parser(self):
13 """Parser factory for broker-specific options.
14
15
16=== modified file 'landscape/broker/exchange.py'
17--- landscape/broker/exchange.py 2010-04-30 08:32:42 +0000
18+++ landscape/broker/exchange.py 2010-04-30 10:32:42 +0000
19@@ -22,7 +22,7 @@
20 plugin_name = "message-exchange"
21
22 def __init__(self, reactor, store, transport, registration_info,
23- message_context_store,
24+ exchange_store,
25 exchange_interval=60*60,
26 urgent_exchange_interval=10,
27 monitor_interval=None,
28@@ -53,7 +53,7 @@
29 self._client_accepted_types = set()
30 self._client_accepted_types_hash = None
31 self._message_handlers = {}
32- self._store = message_context_store
33+ self._exchange_store = exchange_store
34
35 self.register_message("accepted-types", self._handle_accepted_types)
36 self.register_message("resynchronize", self._handle_resynchronize)
37@@ -75,7 +75,7 @@
38 return False
39
40 operation_id = message['operation-id']
41- context = self._store.get_message_context(operation_id)
42+ context = self._exchange_store.get_message_context(operation_id)
43 if context is None:
44 logging.warning(
45 "No message context for message with operation-id: %s"
46@@ -103,13 +103,13 @@
47 "because the client's secure ID has changed in the meantime"
48 % message.get('operation-id'))
49 return None
50- else:
51- if "timestamp" not in message:
52- message["timestamp"] = int(self._reactor.time())
53- message_id = self._message_store.add(message)
54- if urgent:
55- self.schedule_exchange(urgent=True)
56- return message_id
57+
58+ if "timestamp" not in message:
59+ message["timestamp"] = int(self._reactor.time())
60+ message_id = self._message_store.add(message)
61+ if urgent:
62+ self.schedule_exchange(urgent=True)
63+ return message_id
64
65 def start(self):
66 """Start scheduling exchanges. The first one will be urgent."""
67@@ -400,7 +400,7 @@
68 if 'operation-id' in message:
69 # This is a message that requires a response. Store the secure ID
70 # so we can check for obsolete results later.
71- self._store.add_message_context(
72+ self._exchange_store.add_message_context(
73 message['operation-id'], self._registration_info.secure_id,
74 message['type'])
75
76
77=== modified file 'landscape/broker/service.py'
78--- landscape/broker/service.py 2010-04-29 15:57:29 +0000
79+++ landscape/broker/service.py 2010-04-30 10:39:43 +0000
80@@ -53,8 +53,7 @@
81 self.message_store = get_default_message_store(
82 self.persist, config.message_store_path)
83 self.identity = Identity(self.config, self.persist)
84- exchange_store = ExchangeStore(
85- os.path.join(config.data_path, "exchange.database"))
86+ exchange_store = ExchangeStore(self.config.exchange_store_path)
87 self.exchanger = MessageExchange(
88 self.reactor, self.message_store, self.transport, self.identity,
89 exchange_store, config.exchange_interval,
90
91=== modified file 'landscape/broker/tests/helpers.py'
92--- landscape/broker/tests/helpers.py 2010-04-29 15:57:29 +0000
93+++ landscape/broker/tests/helpers.py 2010-04-30 10:44:26 +0000
94@@ -75,11 +75,11 @@
95 test_case.transport = FakeTransport(test_case.config.url,
96 test_case.config.ssl_public_key)
97 test_case.reactor = FakeReactor()
98- exchange_store = ExchangeStore(
99+ test_case.exchange_store = ExchangeStore(
100 os.path.join(test_case.config.data_path, "exchange.database"))
101 test_case.exchanger = MessageExchange(
102 test_case.reactor, test_case.mstore, test_case.transport,
103- test_case.identity, exchange_store,
104+ test_case.identity, test_case.exchange_store,
105 test_case.config.exchange_interval,
106 test_case.config.urgent_exchange_interval)
107
108
109=== modified file 'landscape/broker/tests/test_exchange.py'
110--- landscape/broker/tests/test_exchange.py 2010-04-30 08:29:41 +0000
111+++ landscape/broker/tests/test_exchange.py 2010-04-30 10:48:10 +0000
112@@ -273,10 +273,8 @@
113 Immediately after registration, an urgent exchange should be scheduled.
114 """
115 transport = FakeTransport()
116- exchange_store = ExchangeStore(
117- os.path.join(self.config.data_path, "exchange.database"))
118 exchanger = MessageExchange(self.reactor, self.mstore, transport,
119- self.identity, exchange_store)
120+ self.identity, self.exchange_store)
121 exchanger.start()
122 self.wait_for_exchange(urgent=True)
123 self.assertEquals(len(transport.payloads), 1)
124@@ -502,10 +500,8 @@
125 the total-messages is equivalent to the total number of messages
126 pending.
127 """
128- exchange_store = ExchangeStore(
129- os.path.join(self.config.data_path, "exchange.database"))
130 exchanger = MessageExchange(self.reactor, self.mstore, self.transport,
131- self.identity, exchange_store,
132+ self.identity, self.exchange_store,
133 max_messages=1)
134 self.mstore.set_accepted_types(["empty"])
135 self.mstore.add({"type": "empty"})
136@@ -534,10 +530,8 @@
137 # We create our own MessageExchange because the one set up by the text
138 # fixture has an urgent exchange interval of 10 seconds, which makes
139 # testing this awkward.
140- exchange_store = ExchangeStore(
141- os.path.join(self.config.data_path, "exchange.database"))
142 exchanger = MessageExchange(self.reactor, self.mstore, self.transport,
143- self.identity, exchange_store,
144+ self.identity, self.exchange_store,
145 urgent_exchange_interval=20)
146 exchanger.schedule_exchange(urgent=True)
147 events = []
148@@ -554,10 +548,8 @@
149 should be cancelled and a new one should be scheduled for 10 seconds
150 before the new urgent exchange.
151 """
152- exchange_store = ExchangeStore(
153- os.path.join(self.config.data_path, "exchange.database"))
154 exchanger = MessageExchange(self.reactor, self.mstore, self.transport,
155- self.identity, exchange_store,
156+ self.identity, self.exchange_store,
157 urgent_exchange_interval=20)
158 events = []
159 self.reactor.call_on("impending-exchange", lambda: events.append(True))
160@@ -808,8 +800,9 @@
161 [message] = messages
162 self.assertIsNot(
163 None,
164- self.exchanger._store.get_message_context(message['operation-id']))
165- message_context = self.exchanger._store.get_message_context(message['operation-id'])
166+ self.exchange_store.get_message_context(message['operation-id']))
167+ message_context = self.exchange_store.get_message_context(
168+ message['operation-id'])
169 self.assertEquals(message_context.operation_id, 123456)
170 self.assertEquals(message_context.message_type, "type-R")
171
172@@ -818,14 +811,14 @@
173 Incoming messages without an 'operation-id' key will *not* have the
174 secure id stored in the L{ExchangeStore}.
175 """
176- ids_before = self.exchanger._store.all_operation_ids()
177+ ids_before = self.exchange_store.all_operation_ids()
178
179 msg = {"type": "type-R", "whatever": 5678}
180 server_message = [msg]
181 self.transport.responses.append(server_message)
182 self.exchanger.exchange()
183
184- ids_after = self.exchanger._store.all_operation_ids()
185+ ids_after = self.exchange_store.all_operation_ids()
186 self.assertEquals(ids_before, ids_after)
187
188 def test_obsolete_response_messages_are_discarded(self):
189@@ -844,7 +837,7 @@
190
191 # Change the secure ID so that the response message gets discarded.
192 self.identity.secure_id = 'brand-new'
193- ids_before = self.exchanger._store.all_operation_ids()
194+ ids_before = self.exchange_store.all_operation_ids()
195
196 self.mstore.set_accepted_types(["resynchronize"])
197 message_id = self.exchanger.send(
198@@ -860,7 +853,7 @@
199 self.assertTrue(expected_log_entry in self.logfile.getvalue())
200
201 # The MessageContext was removed after utilisation.
202- ids_after = self.exchanger._store.all_operation_ids()
203+ ids_after = self.exchange_store.all_operation_ids()
204 self.assertTrue(len(ids_after) == len(ids_before) - 1)
205 self.assertFalse('234567' in ids_after)
206

« Back to merge proposal