Ejemplo n.º 1
0
 def subscribe(self):
     if not self.can_read():
         raise ValueError("You don't have read permission")
     request = ReadRequest(self.identity)
     quorum = Quorum(request, self._qs)
     if self.owner:
         quorum.transmit_action(self)
     return request
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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', '*****@*****.**']
        )
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
    def _on_deje_lock_acquire(self, msg, content, ctype, doc):
        lcontent = content['content']
        ltype = lcontent['type']
        if ltype == "deje-event":
            ev_content = lcontent['event']
            ev_version = lcontent['version']
            ev_author  = self.owner.identities.find_by_name(lcontent['author'])

            ev = event.Event(ev_content, ev_author, ev_version)
            quorum = Quorum(ev, doc._qs)
            if doc.can_write(ev_author) and ev.test(doc._current):
                # TODO: Error message for permissions failures and test failures
                quorum.sign(self.owner.identity)
                quorum.transmit(doc, [self.owner.identity.key])
        if ltype == "deje-subscribe":
            rr_subname = lcontent['subscriber']
            subscriber = self.owner.identities.find_by_location(rr_subname)
            #raise Exception(subscriber)
            rr = read.ReadRequest(subscriber)
            quorum = Quorum(rr, doc._qs)
            if doc.can_read(subscriber):
                # TODO: Error message for permissions failures
                quorum.sign(self.owner.identity)
                quorum.transmit(doc)
                quorum.check_enact(doc)
Ejemplo n.º 10
0
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)