def test_no_datawire_token(self): """ If DATAWIRE_TOKEN is not set neither the TracingClient nor the DiscoClient are started. """ # Disable connecting to our Discovery server: runtime = fakeRuntime() runtime.getEnvVarsService().set("MDK_DISCOVERY_SOURCE", "synapse:path=" + mkdtemp()) # Start the MDK: mdk = MDKImpl(runtime) mdk.start() # Do a bunch of logging: session = mdk.session() session.info("category", "hello!") session.error("category", "ono") session.warn("category", "gazoots") session.critical("category", "aaaaaaa") session.debug("category", "behold!") # Time passes... scheduleService = runtime.getScheduleService() for i in range(10): scheduleService.advance(1.0) scheduleService.pump() # No WebSocket connections made: self.assertFalse(runtime.getWebSocketsService().fakeActors)
def setUp(self): """Initialize an empty environment.""" # Initialize runtime and MDK: self.runtime = fakeRuntime() self.runtime.getEnvVarsService().set("DATAWIRE_TOKEN", "something") self.mdk = MDKImpl(self.runtime) self.mdk.start() self.session = self.mdk.session()
def create_mdk_with_faketracer(environment="sandbox"): """Create an MDK with a FakeTracer. Returns (mdk, fake_tracer). """ runtime = fakeRuntime() tracer = FakeTracer() runtime.dependencies.registerService("tracer", tracer) runtime.getEnvVarsService().set("MDK_DISCOVERY_SOURCE", "static:nodes={}") runtime.getEnvVarsService().set("MDK_ENVIRONMENT", environment) mdk = MDKImpl(runtime) mdk.start() return mdk, tracer
def __init__(self, failurepolicy_factory=None, env={}, start=True): self.runtime = fakeRuntime() env_vars = self.runtime.getEnvVarsService() for key, value in env.items(): env_vars.set(key, value) env_vars.set("DATAWIRE_TOKEN", "xxx") env_vars.set("MDK_SERVER_URL", self.URL) if failurepolicy_factory is not None: self.runtime.dependencies.registerService("failurepolicy_factory", failurepolicy_factory) self.mdk = MDKImpl(self.runtime) if start: self.mdk.start() self.pump()
def test_no_tracer(self): """ If no tracer was setup, logging still returns a LoggedMessageId. """ runtime = fakeRuntime() runtime.getEnvVarsService().set("MDK_DISCOVERY_SOURCE", "static:nodes={}") mdk = MDKImpl(runtime) mdk.start() # No DATAWIRE_TOKEN, so no tracer: self.assertEqual(mdk._tracer, None) session = mdk.session() session.info("cat", "message") lmid = session.info("cat", "another message") self.assertEqual((lmid.traceId, lmid.causalLevel), (session._context.traceId, [2]))
def __init__(self, failurepolicy_factory=None, env={}, start=True): self.runtime = fakeRuntime() env_vars = self.runtime.getEnvVarsService() for key, value in env.items(): env_vars.set(key, value) env_vars.set("DATAWIRE_TOKEN", "xxx"); env_vars.set("MDK_SERVER_URL", self.URL); if failurepolicy_factory is not None: self.runtime.dependencies.registerService( "failurepolicy_factory", failurepolicy_factory) self.mdk = MDKImpl(self.runtime) if start: self.mdk.start() self.pump()
class SessionDeadlineTests(TestCase): """Tests for the session deadline.""" def setUp(self): """Initialize an empty environment.""" # Initialize runtime and MDK: self.runtime = fakeRuntime() self.runtime.getEnvVarsService().set("DATAWIRE_TOKEN", "something") self.mdk = MDKImpl(self.runtime) self.mdk.start() self.session = self.mdk.session() def test_setDeadline(self): """A set deadline can be retrieved.""" self.session.setDeadline(13.5) self.assertEqual(13.5, self.session.getRemainingTime()) def test_notSetDeadline(self): """Deadline is null if not set.""" self.assertEqual(None, self.session.getRemainingTime()) def test_deadlineChangesAsTimePasses(self): """If time passes the deadline goes down.""" self.session.setDeadline(13.5) self.runtime.getTimeService().advance(2.0) self.assertEqual(11.5, self.session.getRemainingTime()) def test_setDeadlineTwice(self): """Deadlines can be decreased by setting, but not increased.""" self.session.setDeadline(10.0) self.session.setDeadline(9.0) decreased = self.session.getRemainingTime() self.session.setDeadline(11.0) still_decreased = self.session.getRemainingTime() self.assertEqual((decreased, still_decreased), (9.0, 9.0)) def test_serialization(self): """A serialized session preserves the deadline.""" self.session.setDeadline(10.0) self.session.setProperty("xx", "yy") serialized = self.session.externalize() session2 = self.mdk.join(serialized) self.assertEqual(session2.getRemainingTime(), 10.0) def test_mdkDefault(self): """The MDK can set a default deadline for new sessions.""" self.mdk.setDefaultDeadline(5.0) session = self.mdk.session() self.assertEqual(session.getRemainingTime(), 5.0) def test_mdkDefaultForJoinedSessions(self): """ Deadlines for joined sessions are decreased to the MDK default deadline, but never increased. """ session1 = self.mdk.session() session1.setDeadline(1.0) encoded1 = session1.externalize() session2 = self.mdk.session() session2.setDeadline(3.0) encoded2 = session2.externalize() self.mdk.setDefaultDeadline(2.0) self.assertEqual((1.0, 2.0), (self.mdk.join(encoded1).getRemainingTime(), self.mdk.join(encoded2).getRemainingTime())) def test_resolveNoDeadline(self): """ If a deadline higher than 10 seconds was set, resolving still times out after 10.0 seconds. """ self.session.setDeadline(20.0) start = time() with self.assertRaises(Exception): self.session.resolve("unknown", "1.0") self.assertAlmostEqual(time() - start, 10.0, delta=1) def test_resolveLowerDeadline(self): """ If a deadline lower than 10 seconds was set, resolving happens after the lower deadline. """ self.session.setDeadline(3.0) start = time() with self.assertRaises(Exception): self.session.resolve("unknown", "1.0") self.assertAlmostEqual(time() - start, 3.0, delta=1)
class MDKConnector(object): """Manage an interaction with fake remote server.""" URL = "ws://localhost:1234/" def __init__(self, failurepolicy_factory=None, env={}, start=True): self.runtime = fakeRuntime() env_vars = self.runtime.getEnvVarsService() for key, value in env.items(): env_vars.set(key, value) env_vars.set("DATAWIRE_TOKEN", "xxx") env_vars.set("MDK_SERVER_URL", self.URL) if failurepolicy_factory is not None: self.runtime.dependencies.registerService("failurepolicy_factory", failurepolicy_factory) self.mdk = MDKImpl(self.runtime) if start: self.mdk.start() self.pump() def pump(self): """Deliver scheduled events.""" # Keep pumping the clock and message delivery until no new # messages are available. while True: self.runtime.getTimeService().pump() if self.runtime.dispatcher._queued: self.runtime.dispatcher.pump() else: break def advance_time(self, seconds): """Advance the clock.""" ts = self.runtime.getTimeService() ts.advance(seconds) self.pump() def expectSocket(self): """Return the FakeWSActor we expect to have connected to a URL.""" ws = self.runtime.getWebSocketsService() actor = ws.lastConnection() assert actor.url.startswith(self.URL) return actor def expectSerializable(self, fake_wsactor, expected_type): """Return the last sent message of given type.""" msg = fake_wsactor.expectTextMessage() assert msg is not None json = loads(msg) evt = Serializable.decodeClassName(expected_type, msg) assert json["type"] == evt._json_type return evt def connect(self, fake_wsactor): """Connect and then return the last sent Open message.""" fake_wsactor.accept() self.pump() return self.expectSerializable(fake_wsactor, "mdk_protocol.Open") def expectInteraction(self, test, fake_wsactor, session, failed_nodes, succeeded_nodes): """Assert an InteractionEvent was sent, and return it.""" interaction = self.expectSerializable(fake_wsactor, "mdk_metrics.InteractionEvent") test.assertEqual(interaction.node, self.mdk.procUUID) test.assertEqual(interaction.session, session._context.traceId) expected = {node.getId(): 1 for node in succeeded_nodes} for node in failed_nodes: expected[node.getId()] = 0 test.assertEqual(interaction.results, expected) return interaction
class MDKConnector(object): """Manage an interaction with fake remote server.""" URL = "ws://localhost:1234/" def __init__(self, failurepolicy_factory=None, env={}, start=True): self.runtime = fakeRuntime() env_vars = self.runtime.getEnvVarsService() for key, value in env.items(): env_vars.set(key, value) env_vars.set("DATAWIRE_TOKEN", "xxx"); env_vars.set("MDK_SERVER_URL", self.URL); if failurepolicy_factory is not None: self.runtime.dependencies.registerService( "failurepolicy_factory", failurepolicy_factory) self.mdk = MDKImpl(self.runtime) if start: self.mdk.start() self.pump() def pump(self): """Deliver scheduled events.""" # Keep pumping the clock and message delivery until no new # messages are available. while True: self.runtime.getTimeService().pump() if self.runtime.dispatcher._queued: self.runtime.dispatcher.pump() else: break def advance_time(self, seconds): """Advance the clock.""" ts = self.runtime.getTimeService() ts.advance(seconds) self.pump() def expectSocket(self): """Return the FakeWSActor we expect to have connected to a URL.""" ws = self.runtime.getWebSocketsService() actor = ws.lastConnection() assert actor.url.startswith(self.URL) return actor def expectSerializable(self, fake_wsactor, expected_type): """Return the last sent message of given type.""" msg = fake_wsactor.expectTextMessage() assert msg is not None json = loads(msg) evt = Serializable.decodeClassName(expected_type, msg) assert json["type"] == evt._json_type return evt def connect(self, fake_wsactor): """Connect and then return the last sent Open message.""" fake_wsactor.accept() self.pump() return self.expectSerializable(fake_wsactor, "mdk_protocol.Open") def expectInteraction(self, test, fake_wsactor, session, failed_nodes, succeeded_nodes): """Assert an InteractionEvent was sent, and return it.""" interaction = self.expectSerializable( fake_wsactor, "mdk_metrics.InteractionEvent") test.assertEqual(interaction.node, self.mdk.procUUID) test.assertEqual(interaction.session, session._context.traceId) expected = {node.getId(): 1 for node in succeeded_nodes} for node in failed_nodes: expected[node.getId()] = 0 test.assertEqual(interaction.results, expected) return interaction