class TestLuaHandler(StreamTest):
    def setUp(self):
        StreamTest.setUp(self)

        self.doc = handler_document(self.name)
        self.identity = Identity("*****@*****.**", ["rotate", 5], ["local", None, "kirk"])
        self.owner = Owner(self.identity, make_jack=False)
        self.owner.own_document(self.doc)
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)
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 test_init(self):
        # Do setup for testing a good owner.
        owner = Owner(identity(), make_jack = False)

        key = String('["local",null,"mitzi"]')
        self.assertIsInstance(owner.identities, IdentityCache)
        self.assertIn(key, owner.identities)
        self.assertIsInstance(owner.identities[key], Identity)

        doc = handler_document("echo_chamber")
        self.assertIsInstance(doc.handler, Resource)
        owner.own_document(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', '*****@*****.**']
        )
Exemple #7
0
class TestEJTP(StreamTest):
    def setUp(self):
        StreamTest.setUp(self)

        self.router = Router()
        self.mitzi = Owner(identity("mitzi"), self.router)
        self.atlas = Owner(identity("atlas"), self.router)
        self.victor = Owner(identity("victor"), self.router)
        self.mitzi.identities.sync(self.atlas.identities, self.victor.identities)

        # Document that mitzi and atlas are part of, but victor is not.
        # Separate identical starting points for all of them.
        self.mdoc = handler_document("tag_team")
        self.adoc = handler_document("tag_team")
        self.vdoc = handler_document("tag_team")
        self.mitzi.own_document(self.mdoc)
        self.atlas.own_document(self.adoc)
        self.victor.own_document(self.vdoc)
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)