class TestQuorumSpace(unittest.TestCase): def setUp(self): self.doc = handler_document("tag_team") self.qs = self.doc._qs self.mitzi = identity('mitzi') self.atlas = identity('atlas') self.ev1 = Event({"hello":"world"}, self.mitzi, self.doc.version) self.ev2 = Event({"turtle":"food"}, self.mitzi, self.doc.version) self.q1 = Quorum(self.ev1, self.qs) self.q2 = Quorum(self.ev2, self.qs) self.owner = Owner(self.mitzi, make_jack=False) self.owner.own_document(self.doc) self.owner.identities.update_ident(self.atlas) def test_initial_state(self): self.assertEqual( sorted(self.qs.participants, key = lambda x: x.key), [self.atlas, self.mitzi] ) self.assertTrue(self.qs.is_free(self.mitzi)) self.assertTrue(self.qs.is_free(self.atlas)) self.assertTrue(self.q1.competing) def test_double_sign(self): # TODO: Break this apart when I better understand the side # effects that this test depends on. self.q1.sign(self.mitzi) self.assertFalse(self.qs.is_free(self.mitzi)) self.assertTrue(self.qs.is_free(self.atlas)) self.assertEqual(len(self.qs.by_hash), 2) self.assertIsInstance(self.qs.by_hash[self.ev1.hash()], Quorum) self.assertIsInstance(self.qs.by_hash[self.ev2.hash()], Quorum) self.assertEquals( self.qs.by_author, {self.mitzi.key: self.q1} ) self.assertTrue(self.q1.competing) self.assertFalse(self.q1.done) # Not double signing, same person and ev self.q1.sign(self.mitzi) # Double signing, same person but different ev self.assertRaises( QSDoubleSigning, self.q2.sign, self.mitzi ) self.q1.sign(self.atlas) self.assertTrue(self.qs.is_free(self.mitzi)) self.assertTrue(self.qs.is_free(self.atlas)) self.assertFalse(self.q1.competing) self.assertTrue(self.q1.done)
def test_apply(self): res = handler_resource("tag_team") hs = HistoryState("example", [res]) ev = Event( {"path": "/handler.lua", "property": "comment", "value": "An arbitrary comment"}, identity("mitzi"), None ) hs.apply(ev) self.assertEqual(res.comment, "An arbitrary comment") self.assertEqual(hs.hash, ev.hash())
def setUp(self): self.doc = handler_document("tag_team") self.qs = self.doc._qs self.mitzi = identity('mitzi') self.atlas = identity('atlas') self.ev1 = Event({"hello":"world"}, self.mitzi, self.doc.version) self.ev2 = Event({"turtle":"food"}, self.mitzi, self.doc.version) self.q1 = Quorum(self.ev1, self.qs) self.q2 = Quorum(self.ev2, self.qs) self.owner = Owner(self.mitzi, make_jack=False) self.owner.own_document(self.doc) self.owner.identities.update_ident(self.atlas)
def setUp(self): self.doc = handler_document('echo_chamber') self.ident = identity() self.ev = Event({'x':'y'}, self.ident, 'stormageddon') self.quorum = Quorum(self.ev, self.doc._qs) self.owner = Owner(self.ident, make_jack=False) self.owner.own_document(self.doc)
def setUp(self): self.mitzi = identity('mitzi') self.atlas = identity('atlas') self.res_default = Resource() self.res_tt = handler_resource("tag_team") self.ev_default = Event({'hello':'world'}, self.mitzi) self.ev_tt = Event( { 'path' : '/handler.lua', 'property' : 'comment', 'value' : 'An arbitrary comment', }, self.atlas ) self.hs1 = HistoryState(None, [self.res_default]) self.hs2 = HistoryState("example", [self.res_default, self.res_tt])
class TestQuorum(StreamTest): def setUp(self): StreamTest.setUp(self) self.doc = handler_document("echo_chamber") self.ev = Event({'x':'y'}, identity("atlas"), self.doc.version) self.quorum = Quorum(self.ev, self.doc._qs) self.quorum.document = self.doc self.ident = identity() self.owner = Owner(self.ident, make_jack=False) self.owner.own_document(self.doc) self.owner.identities.update_ident(self.ev.author) def test_clear(self): self.quorum.sign(self.ident) self.assertEqual(self.quorum.completion, 1) self.quorum.clear() self.assertEqual(self.quorum.completion, 0) def test_completion(self): self.assertEqual(self.quorum.completion, 0) self.quorum.sign(self.ident) self.assertEqual(self.quorum.completion, 1) def test_outdated(self): self.assertEqual(self.doc.version, 'current') self.assertEqual(self.doc._qs.version, 'current') self.assertEqual(self.ev.version, 'current') self.assertEqual(self.quorum.version, 'current') self.assertFalse(self.quorum.outdated) self.quorum.sign(self.ident) self.assertFalse(self.quorum.outdated) self.owner.protocol.paxos.check_quorum(self.doc, self.ev) # Stupid Python 2 makes this the easiest way to put up with string crap output = self.getOutput() self.assertIn(output, [ "Event '{'x': 'y'}' achieved.\n", "Event '{u'x': u'y'}' achieved.\n", ]) self.assertEqual(self.doc.version, self.ev.hash()) self.assertEqual(self.quorum.version, 'current') self.assertTrue(self.quorum.outdated) def test_participants(self): self.assertEqual(self.quorum.participants, [self.ident]) def test_thresholds(self): self.assertEqual(self.quorum.thresholds, {'read':1, 'write':1}) def test_threshold(self): self.assertEqual(self.quorum.threshold, 1)
def setUp(self): StreamTest.setUp(self) self.doc = handler_document("echo_chamber") self.ev = Event({'x':'y'}, identity("atlas"), self.doc.version) self.quorum = Quorum(self.ev, self.doc._qs) self.quorum.document = self.doc self.ident = identity() self.owner = Owner(self.ident, make_jack=False) self.owner.own_document(self.doc)
class TestQuorum(StreamTest): def setUp(self): StreamTest.setUp(self) self.doc = handler_document("echo_chamber") self.ev = Event({'x':'y'}, identity("atlas"), self.doc.version) self.quorum = Quorum(self.ev, self.doc._qs) self.quorum.document = self.doc self.ident = identity() self.owner = Owner(self.ident, make_jack=False) self.owner.own_document(self.doc) def test_clear(self): self.quorum.sign(self.ident) self.assertEqual(self.quorum.completion, 1) self.quorum.clear() self.assertEqual(self.quorum.completion, 0) def test_completion(self): self.assertEqual(self.quorum.completion, 0) self.quorum.sign(self.ident) self.assertEqual(self.quorum.completion, 1) def test_outdated(self): self.assertEqual(self.doc.version, 'current') self.assertEqual(self.doc._qs.version, 'current') self.assertEqual(self.ev.version, 'current') self.assertEqual(self.quorum.version, 'current') self.assertFalse(self.quorum.outdated) self.quorum.sign(self.ident) self.assertFalse(self.quorum.outdated) self.quorum.enact(self.doc) self.assertOutput( "Event '{'x': 'y'}' achieved.\n" + "No known address for String('[\"local\",null,\"atlas\"]'), skipping\n" ) self.assertEqual(self.doc.version, self.ev.hash()) self.assertEqual(self.quorum.version, 'current') self.assertTrue(self.quorum.outdated) def test_participants(self): self.assertEqual(self.quorum.participants, [self.ident]) def test_thresholds(self): self.assertEqual(self.quorum.thresholds, {'read':1, 'write':1}) def test_threshold(self): self.assertEqual(self.quorum.threshold, 1)
class TestEvent(unittest.TestCase): def setUp(self): self.doc = handler_document('echo_chamber') self.ident = identity() self.ev = Event({'x':'y'}, self.ident, 'stormageddon') self.quorum = Quorum(self.ev, self.doc._qs) self.owner = Owner(self.ident, make_jack=False) self.owner.own_document(self.doc) def test_init(self): self.assertEqual(self.ev.version, 'stormageddon') self.quorum.sign(self.ident) self.assertEqual( self.quorum.participants, [self.ident] ) self.assertTrue(self.quorum.sig_valid(self.ident.key)) self.assertRaises( TypeError, self.quorum.sign, "some string" ) self.assertFalse(self.quorum.sig_valid("some string")) def test_authorname(self): # Override ev for this test self.ev = Event(None, self.owner.identity) self.assertTrue( isinstance(self.ev.author, Identity) ) self.assertEqual( self.ev.authorname, '*****@*****.**' ) self.assertEqual( self.doc.identity.name, '*****@*****.**' ) def test_hash(self): self.assertEqual( self.ev.hash(), String('92395fb63e777d342d75915ad25dde1172e20f7c') ) def test_hashcontent(self): self.assertEqual( self.ev.hashcontent, [{'x': 'y'}, 'stormageddon', '*****@*****.**'] )
def test_authorname(self): # Override ev for this test self.ev = Event(None, self.owner.identity) self.assertTrue( isinstance(self.ev.author, Identity) ) self.assertEqual( self.ev.authorname, '*****@*****.**' ) self.assertEqual( self.doc.identity.name, '*****@*****.**' )
class TestHistory(unittest.TestCase): def setUp(self): self.mitzi = identity('mitzi') self.atlas = identity('atlas') self.res_default = Resource() self.res_tt = handler_resource("tag_team") self.ev_default = Event({'hello':'world'}, self.mitzi) self.ev_tt = Event( { 'path' : '/handler.lua', 'property' : 'comment', 'value' : 'An arbitrary comment', }, self.atlas ) self.hs1 = HistoryState(None, [self.res_default]) self.hs2 = HistoryState("example", [self.res_default, self.res_tt]) def test_init(self): hist = History() self.assertEqual(hist.states, {}) self.assertEqual(hist.events, []) self.assertEqual(hist.events_by_hash, {}) def test_init_states(self): hist = History([self.hs1, self.hs2]) self.assertEqual(hist.states, { None : self.hs1, "example" : self.hs2, }) self.assertEqual(hist.events, []) self.assertEqual(hist.events_by_hash, {}) def test_init_events(self): hist = History(events=[self.ev_default, self.ev_tt]) self.assertEqual(hist.states, {}) self.assertEqual(hist.events, [self.ev_default, self.ev_tt]) self.assertEqual(hist.events_by_hash, { self.ev_default.hash(): self.ev_default, self.ev_tt.hash(): self.ev_tt, }) def test_init_both(self): hist = History([self.hs1, self.hs2], [self.ev_default, self.ev_tt]) self.assertEqual(hist.states, { None : self.hs1, "example" : self.hs2, }) self.assertEqual(hist.events, [self.ev_default, self.ev_tt]) self.assertEqual(hist.events_by_hash, { self.ev_default.hash(): self.ev_default, self.ev_tt.hash(): self.ev_tt, }) def test_orphan_states(self): hist = History() self.assertEqual(hist.orphan_states, []) hist.add_event(self.ev_default) self.assertEqual(hist.orphan_states, []) self.hs1.hash = self.ev_default.hash() hist.add_states([self.hs1, self.hs2]) self.assertEqual(hist.orphan_states, [self.hs2]) def test_orphan_events(self): hist = History() self.assertEqual(hist.orphan_events, []) hist.add_event(self.ev_default) self.assertEqual(hist.orphan_events, [self.ev_default]) self.hs1.hash = self.ev_default.hash() hist.add_states([self.hs1, self.hs2]) self.assertEqual(hist.orphan_events, []) hist.add_event(self.ev_tt) self.assertEqual(hist.orphan_events, [self.ev_tt])
class TestEvent(unittest.TestCase): def setUp(self): self.doc = handler_document('echo_chamber') self.ident = identity() self.ev = Event({'x':'y'}, self.ident, 'stormageddon') self.quorum = Quorum(self.ev, self.doc._qs) self.owner = Owner(self.ident, make_jack=False) self.owner.own_document(self.doc) self.io = IOMock() def test_init(self): self.assertEqual(self.ev.version, 'stormageddon') self.quorum.sign(self.ident) self.assertEqual( self.quorum.participants, [self.ident] ) self.assertTrue(self.quorum.sig_valid(self.ident.key)) self.assertRaises( TypeError, self.quorum.sign, "some string" ) self.assertFalse(self.quorum.sig_valid("some string")) def test_authorname(self): # Override ev for this test self.ev = Event(None, self.owner.identity) self.assertTrue( isinstance(self.ev.author, Identity) ) self.assertEqual( self.ev.authorname, '*****@*****.**' ) self.assertEqual( self.doc.identity.name, '*****@*****.**' ) def test_hash(self): self.assertEqual( self.ev.hash(), String('ce9950bb5a8db63712ae1ecfe9269e22289673ec') ) def test_serialize(self): self.assertEqual( self.ev.serialize(), { 'type': 'event', 'author': ['local', None, 'mitzi'], 'content': {'x':'y'}, 'version': 'stormageddon', } ) def test_enact(self): self.assertEqual(self.ev.is_done(self.doc), False) with self.io: self.ev.enact(self.quorum, self.doc) self.assertEqual(self.ev.is_done(self.doc), True)