def done(res): self.result.ended = perf_counter() self.result.log.append( (self.result.ended, None, None, "Test ended.")) clientErrors = [] for r in res: if not r[0]: clientErrors.append(str(r[1].value)) if len(clientErrors) > 0: passed = False print "Client errors", clientErrors else: passed = json.dumps(self.result.observed) == json.dumps( self.result.expected) if False and not passed: print print "EXPECTED" print self.result.expected print "OBSERVED" print self.result.observed print self.result.passed = passed self.finished.callback(self.result)
def main(self): subscribeTopics = self.test.params.peers[self.factory.peerIndex] for topic in subscribeTopics: topic += self.factory.test._uriSuffix self.subscribe(topic, self.onEvent) self.test.result.log.append((perf_counter(), self.factory.peerIndex, self.session_id, "Subscribed to <pre>%s</pre>" % topic)) self.factory.onReady.callback(self.session_id)
def onSessionOpen(self): self.test.result.log.append((perf_counter(), self.factory.peerIndex, self.session_id, "WAMP session opened to <strong>%s</strong> at <strong>%s</strong>." % (self.session_server, self.peer))) if self.test.testee.auth: d = self.authenticate(**self.test.testee.auth) d.addCallbacks(self.onAuthSuccess, self.onAuthError) else: self.main()
def launch(_): ## After all our clients have signaled "peersready", these ## clients will just have sent their subscribe WAMP messages, ## and since with WAMPv1, there is no reply (to wait on), the ## clients immediately signal readiness and we need to _wait_ ## here to give the testee time to receive and actually subscribe ## the clients. When we don't wait, we might publish test events ## before the testee has subscribed all clients as needed. ## We need acknowledgement of subscribe for WAMPv2! ## wait = 2.5 * self._rtt def afterwait(): self.result.log.append( (perf_counter(), None, None, "Continuing test ..")) test() self.result.log.append( (perf_counter(), None, None, "Sleeping for <strong>%s ms</strong> ..." % (1000. * wait))) def beforewait(): reactor.callLater(wait, afterwait) reactor.callLater(0, beforewait)
def onEvent(self, topic, event): self.test.result.log.append( (perf_counter(), self.factory.peerIndex, self.session_id, "Received event for topic <pre>%s</pre> and payload <pre>%s</pre>" % (topic, event))) if not self.test.result.observed.has_key(self.session_id): self.test.result.observed[self.session_id] = [] self.test.result.observed[self.session_id].append((topic, event))
def done(proto): self.result.ended = perf_counter() passed = json.dumps(self.result.received) == json.dumps(self.result.expected) if not passed: print "EXPECTED", self.result.expected print "RECEIVED", self.result.received self.result.passed = passed self.finished.callback(self.result)
def clientConnectionLost(self, connector, reason): reason = str(reason.value) if self.proto and hasattr(self.proto, 'session_id'): sid = self.proto.session_id else: sid = None self.test.result.log.append((perf_counter(), self.peerIndex, sid, "Client connection lost: %s" % reason)) self.onGone.callback(None)
def done(proto): self.result.ended = perf_counter() passed = json.dumps(self.result.received) == json.dumps( self.result.expected) if not passed: print "EXPECTED", self.result.expected print "RECEIVED", self.result.received self.result.passed = passed self.finished.callback(self.result)
def launch(_): ## After all our clients have signaled "peersready", these ## clients will just have sent their subscribe WAMP messages, ## and since with WAMPv1, there is no reply (to wait on), the ## clients immediately signal readiness and we need to _wait_ ## here to give the testee time to receive and actually subscribe ## the clients. When we don't wait, we might publish test events ## before the testee has subscribed all clients as needed. ## We need acknowledgement of subscribe for WAMPv2! ## wait = 2.5 * self._rtt def afterwait(): self.result.log.append((perf_counter(), None, None, "Continuing test ..")) test() self.result.log.append((perf_counter(), None, None, "Sleeping for <strong>%s ms</strong> ..." % (1000. * wait))) def beforewait(): reactor.callLater(wait, afterwait) reactor.callLater(0, beforewait)
def done(res): self.result.ended = perf_counter() self.result.log.append((self.result.ended, None, None, "Test ended.")) clientErrors = [] for r in res: if not r[0]: clientErrors.append(str(r[1].value)) if len(clientErrors) > 0: passed = False print "Client errors", clientErrors else: passed = json.dumps(self.result.observed) == json.dumps(self.result.expected) if False and not passed: print print "EXPECTED" print self.result.expected print "OBSERVED" print self.result.observed print self.result.passed = passed self.finished.callback(self.result)
def onres(res): self.result.log.append((perf_counter(), None, None, "Event init call result: %s" % res)) beforewait()
def onMessage(self, payload, binary): self.test.result.log.append((perf_counter(), self.factory.peerIndex, self.session_id, '<pre class="wamp">RX <= %s</pre>' % payload)) WampCraClientProtocol.onMessage(self, payload, binary)
def test(): for c in self.clients: self.result.expected[c.proto.session_id] = [] self.result.observed[c.proto.session_id] = [] publisherPeerIndex = 0 publisher = self.clients[publisherPeerIndex] publisherSessionId = publisher.proto.session_id topic = self.params.publicationTopic + self._uriSuffix payloads = self.params.eventPayloads expectedReceivers = [self.clients[i] for i in self.params.expectedReceivers] for r in expectedReceivers: for p in payloads: self.result.expected[r.proto.session_id].append((topic, p)) args = {} if self.params.excludeMe is not None: args['excludeMe'] = self.params.excludeMe if self.params.exclude is not None: ## map exclude indices to session IDs args['exclude'] = [] for i in self.params.exclude: args['exclude'].append(self.clients[i].proto.session_id) if self.params.eligible is not None: ## map exclude indices to session IDs args['eligible'] = [] for i in self.params.eligible: args['eligible'].append(self.clients[i].proto.session_id) d_pl = [] for pl in payloads: if self.params.publicationMethod == 0: ## publish using standard WAMP event publication ## publisher.proto.publish(topic, pl, **args) elif self.params.publicationMethod == 1: ## publish indirectly by instructing the peer to ## dispatch an event ## args['me'] = publisherSessionId ENDPOINT = "http://api.testsuite.wamp.ws/testee/control#dispatch" #ENDPOINT = "http://api.testsuite.wamp.ws/autobahn/testee/control#dispatch" cd = publisher.proto.call(ENDPOINT, topic, pl, args) del args['me'] # don't show this in test log d_pl.append(cd) else: raise Exception("no such publication method: %s" % self.params.publicationMethod) s_args = ["%s=%s" % (k,v) for (k,v) in args.items()] if len(s_args) > 0: s_args = 'with options <pre>%s</pre> ' % ', '.join(s_args) else: s_args = '' if self.params.publicationMethod == 0: msg = "Published event to topic <pre>%s</pre> %sand payload <pre>%s</pre>" % (topic, s_args, pl) elif self.params.publicationMethod == 1: msg = "Initiated server dispatched event to topic <pre>%s</pre> %sand payload <pre>%s</pre>" % (topic, s_args, pl) else: msg = "" self.result.log.append((perf_counter(), publisherPeerIndex, publisher.proto.session_id, msg)) ## After having published everything the test had specified, ## we need to _wait_ to receive events on all our WAMP sessions ## to compare with our expectation. By default, we wait 3x the ## specified/default RTT. ## wait = 1.5 * self._rtt def afterwait(): self.result.log.append((perf_counter(), None, None, "Continuing test ..")) shutdown() def beforewait(): self.result.log.append((perf_counter(), None, None, "Sleeping for <strong>%s ms</strong> ..." % (1000. * wait))) reactor.callLater(wait, afterwait) if self.params.publicationMethod == 1 and len(d_pl) > 0: d = DeferredList(d_pl) def onres(res): self.result.log.append((perf_counter(), None, None, "Event init call result: %s" % res)) beforewait() d.addCallback(onres) else: #reactor.callLater(0, beforewait) beforewait()
def beforewait(): self.result.log.append((perf_counter(), None, None, "Sleeping for <strong>%s ms</strong> ..." % (1000. * wait))) reactor.callLater(wait, afterwait)
def run(self): self.result.started = perf_counter() self.result.log.append((self.result.started, None, None, "Test started.")) self.clients = [] peersready = [] peersgone = [] i = 1 for peerIndex in xrange(len(self.params.peers)): ready = Deferred() gone = Deferred() client = WampCase2_2_x_x_Factory(self, peerIndex, ready, gone) self.clients.append(client) peersready.append(ready) peersgone.append(gone) connectWS(client) i += 1 def shutdown(): for c in self.clients: c.proto.sendClose() self.result.log.append((perf_counter(), c.peerIndex, c.proto.session_id, "Test client closing ...")) def test(): for c in self.clients: self.result.expected[c.proto.session_id] = [] self.result.observed[c.proto.session_id] = [] publisherPeerIndex = 0 publisher = self.clients[publisherPeerIndex] publisherSessionId = publisher.proto.session_id topic = self.params.publicationTopic + self._uriSuffix payloads = self.params.eventPayloads expectedReceivers = [self.clients[i] for i in self.params.expectedReceivers] for r in expectedReceivers: for p in payloads: self.result.expected[r.proto.session_id].append((topic, p)) args = {} if self.params.excludeMe is not None: args['excludeMe'] = self.params.excludeMe if self.params.exclude is not None: ## map exclude indices to session IDs args['exclude'] = [] for i in self.params.exclude: args['exclude'].append(self.clients[i].proto.session_id) if self.params.eligible is not None: ## map exclude indices to session IDs args['eligible'] = [] for i in self.params.eligible: args['eligible'].append(self.clients[i].proto.session_id) d_pl = [] for pl in payloads: if self.params.publicationMethod == 0: ## publish using standard WAMP event publication ## publisher.proto.publish(topic, pl, **args) elif self.params.publicationMethod == 1: ## publish indirectly by instructing the peer to ## dispatch an event ## args['me'] = publisherSessionId ENDPOINT = "http://api.testsuite.wamp.ws/testee/control#dispatch" #ENDPOINT = "http://api.testsuite.wamp.ws/autobahn/testee/control#dispatch" cd = publisher.proto.call(ENDPOINT, topic, pl, args) del args['me'] # don't show this in test log d_pl.append(cd) else: raise Exception("no such publication method: %s" % self.params.publicationMethod) s_args = ["%s=%s" % (k,v) for (k,v) in args.items()] if len(s_args) > 0: s_args = 'with options <pre>%s</pre> ' % ', '.join(s_args) else: s_args = '' if self.params.publicationMethod == 0: msg = "Published event to topic <pre>%s</pre> %sand payload <pre>%s</pre>" % (topic, s_args, pl) elif self.params.publicationMethod == 1: msg = "Initiated server dispatched event to topic <pre>%s</pre> %sand payload <pre>%s</pre>" % (topic, s_args, pl) else: msg = "" self.result.log.append((perf_counter(), publisherPeerIndex, publisher.proto.session_id, msg)) ## After having published everything the test had specified, ## we need to _wait_ to receive events on all our WAMP sessions ## to compare with our expectation. By default, we wait 3x the ## specified/default RTT. ## wait = 1.5 * self._rtt def afterwait(): self.result.log.append((perf_counter(), None, None, "Continuing test ..")) shutdown() def beforewait(): self.result.log.append((perf_counter(), None, None, "Sleeping for <strong>%s ms</strong> ..." % (1000. * wait))) reactor.callLater(wait, afterwait) if self.params.publicationMethod == 1 and len(d_pl) > 0: d = DeferredList(d_pl) def onres(res): self.result.log.append((perf_counter(), None, None, "Event init call result: %s" % res)) beforewait() d.addCallback(onres) else: #reactor.callLater(0, beforewait) beforewait() def launch(_): ## After all our clients have signaled "peersready", these ## clients will just have sent their subscribe WAMP messages, ## and since with WAMPv1, there is no reply (to wait on), the ## clients immediately signal readiness and we need to _wait_ ## here to give the testee time to receive and actually subscribe ## the clients. When we don't wait, we might publish test events ## before the testee has subscribed all clients as needed. ## We need acknowledgement of subscribe for WAMPv2! ## wait = 2.5 * self._rtt def afterwait(): self.result.log.append((perf_counter(), None, None, "Continuing test ..")) test() self.result.log.append((perf_counter(), None, None, "Sleeping for <strong>%s ms</strong> ..." % (1000. * wait))) def beforewait(): reactor.callLater(wait, afterwait) reactor.callLater(0, beforewait) def error(err): ## FIXME print "ERROR", err shutdown() self.finished.errback(err) def done(res): self.result.ended = perf_counter() self.result.log.append((self.result.ended, None, None, "Test ended.")) clientErrors = [] for r in res: if not r[0]: clientErrors.append(str(r[1].value)) if len(clientErrors) > 0: passed = False print "Client errors", clientErrors else: passed = json.dumps(self.result.observed) == json.dumps(self.result.expected) if False and not passed: print print "EXPECTED" print self.result.expected print "OBSERVED" print self.result.observed print self.result.passed = passed self.finished.callback(self.result) DeferredList(peersready).addCallbacks(launch, error) DeferredList(peersgone).addCallbacks(done, error) self.finished = Deferred() return self.finished
def shutdown(): for c in self.clients: c.proto.sendClose() self.result.log.append((perf_counter(), c.peerIndex, c.proto.session_id, "Test client closing ..."))
def log(self, msg): ts = perf_counter() sessionId = self.proto.session_id if self.proto else None self.result.log.append((ts, self.peerIndex, sessionId, msg.encode('utf8'))) return ts
def clientConnectionFailed(self, connector, reason): reason = str(reason.value) self.test.result.log.append((perf_counter(), self.peerIndex, None, "Client connection failed: %s" % reason)) self.onGone.callback(reason)
def log(msg, sessionIndex = None, sessionId = None): ts = perf_counter() result.log.append((ts, sessionIndex, sessionId, msg.encode('utf8'))) return ts
def onEvent(self, topic, event): self.test.result.log.append((perf_counter(), self.factory.peerIndex, self.session_id, "Received event for topic <pre>%s</pre> and payload <pre>%s</pre>" % (topic, event))) if not self.test.result.observed.has_key(self.session_id): self.test.result.observed[self.session_id] = [] self.test.result.observed[self.session_id].append((topic, event))
def onAuthError(self, e): uri, desc, details = e.value.args self.test.result.log.append((perf_counter(), self.factory.peerIndex, self.session_id, "WAMP authentication error: %s" % details)) print "Authentication Error!", uri, desc, details
def run(self): self.result.started = perf_counter() def shutdown(): if self.client: self.client.proto.sendClose() def test(proto): #res = yield self.call("http://api.testsuite.wamp.ws/case/3.1.1#1", 23) ## after having published everything the test had specified, ## we need to _wait_ for events on all our WAMP sessions to ## compare with our expectation. by default, we wait 3x the ## specified/default RTT def perform(i, p): d = proto.call("http://api.testsuite.wamp.ws/case/3.1.1#1", float(p)) def got(res): self.result.received[i] = float(res) d.addCallback(got) payloads = [] payloads.extend([0]) payloads.extend([2**7 - 1, 2**8 - 1, 2**15 - 1, 2**16 - 1, 2**24]) #payloads.extend([2**7-1, 2**8-1, 2**15-1, 2**16-1, 2**24, 2**31-1, 2**32-1, 2**53]) #payloads.extend([2**53+1, 2**63-1, 2**64-1]) #payloads.extend([-2**7, -2**15, -2**24, -2**31, -2**53]) payloads.extend([-2**7, -2**15, -2**24]) #payloads.extend([-2**63]) i = 0 for p in payloads: self.result.expected[i] = float(p) perform(i, p) i += 1 wait = 3 * self.testee.options.get("rtt", 0.2) reactor.callLater(wait, shutdown) def launch(proto): ## FIXME: explain why the following needed, since ## without the almost zero delay (which triggers a ## reactor loop), the code will not work as expected! #test() # <= does NOT work reactor.callLater(0.00001, test, proto) def error(err): ## FIXME print "ERROR", err shutdown() self.finished.errback(err) def done(proto): self.result.ended = perf_counter() passed = json.dumps(self.result.received) == json.dumps( self.result.expected) if not passed: print "EXPECTED", self.result.expected print "RECEIVED", self.result.received self.result.passed = passed self.finished.callback(self.result) self.client = WampCase3_1_x_x_Factory(self, launch, done) connectWS(self.client) self.finished = Deferred() return self.finished
def afterwait(): self.result.log.append((perf_counter(), None, None, "Continuing test ..")) test()
def onAuthSuccess(self, permissions): self.test.result.log.append((perf_counter(), self.factory.peerIndex, self.session_id, "WAMP session %s authenticated with credentials: <pre>%s</pre>" % (self.session_id, self.test.testee.auth))) self.main()
def run(self): self.result.started = perf_counter() def shutdown(): if self.client: self.client.proto.sendClose() def test(proto): #res = yield self.call("http://api.testsuite.wamp.ws/case/3.1.1#1", 23) ## after having published everything the test had specified, ## we need to _wait_ for events on all our WAMP sessions to ## compare with our expectation. by default, we wait 3x the ## specified/default RTT def perform(i, p): d = proto.call("http://api.testsuite.wamp.ws/case/3.1.1#1", float(p)) def got(res): self.result.received[i] = float(res) d.addCallback(got) payloads = [] payloads.extend([0]) payloads.extend([2**7-1, 2**8-1, 2**15-1, 2**16-1, 2**24]) #payloads.extend([2**7-1, 2**8-1, 2**15-1, 2**16-1, 2**24, 2**31-1, 2**32-1, 2**53]) #payloads.extend([2**53+1, 2**63-1, 2**64-1]) #payloads.extend([-2**7, -2**15, -2**24, -2**31, -2**53]) payloads.extend([-2**7, -2**15, -2**24]) #payloads.extend([-2**63]) i = 0 for p in payloads: self.result.expected[i] = float(p) perform(i, p) i += 1 wait = 3 * self.testee.options.get("rtt", 0.2) reactor.callLater(wait, shutdown) def launch(proto): ## FIXME: explain why the following needed, since ## without the almost zero delay (which triggers a ## reactor loop), the code will not work as expected! #test() # <= does NOT work reactor.callLater(0.00001, test, proto) def error(err): ## FIXME print "ERROR", err shutdown() self.finished.errback(err) def done(proto): self.result.ended = perf_counter() passed = json.dumps(self.result.received) == json.dumps(self.result.expected) if not passed: print "EXPECTED", self.result.expected print "RECEIVED", self.result.received self.result.passed = passed self.finished.callback(self.result) self.client = WampCase3_1_x_x_Factory(self, launch, done) connectWS(self.client) self.finished = Deferred() return self.finished