Skip to content

Commit

Permalink
fix test_cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
LipuFei committed Jan 21, 2015
1 parent c7fdd05 commit 162c809
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions tests/test_cancel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest import skip

from .dispersytestclass import DispersyTestFunc
from ..util import blocking_call_on_reactor_thread

Expand All @@ -19,15 +17,15 @@ def test_self_cancel_own(self):
messages = [node.create_full_sync_text("Should cancel #%d" % i, i + 10) for i in xrange(10)]
node.give_messages(messages, node)

# check that they are in the database and are NOT undone
# check that they are in the database and are NOT canceled
node.assert_is_stored(messages=messages)

# undo all messages
cancels = [node.create_undo_own(message, i + 100) for i, message in enumerate(messages)]
# cancel all messages
cancels = [node.create_cancel_own(message, i + 100) for i, message in enumerate(messages)]

node.give_messages(cancels, node)

# check that they are in the database and ARE undone
# check that they are in the database and ARE canceled
node.assert_is_canceled(messages=messages)
node.assert_is_stored(messages=cancels)

Expand All @@ -39,7 +37,7 @@ def test_node_cancel_other(self):
node, other = self.create_nodes(2)
other.send_identity(node)

# MM grants undo permission to NODE
# MM grants cancel permission to NODE
authorize = self._mm.create_authorize([(node.my_member, self._community.get_meta_message(u"full-sync-text"),
u"cancel")], self._mm.claim_global_time())
node.give_message(authorize, self._mm)
Expand All @@ -49,15 +47,15 @@ def test_node_cancel_other(self):
messages = [other.create_full_sync_text("Should cancel #%d" % i, i + 10) for i in xrange(10)]
node.give_messages(messages, other)

# check that they are in the database and are NOT undone
# check that they are in the database and are NOT canceled
node.assert_is_stored(messages=messages)

# NODE cancels all messages
cancels = [node.create_cancel_other(message, message.distribution.global_time + 100)
for i, message in enumerate(messages)]
node.give_messages(cancels, node)

# check that they are in the database and ARE undone
# check that they are in the database and ARE canceled
node.assert_is_canceled(messages=messages)
node.assert_is_stored(messages=cancels)

Expand All @@ -80,17 +78,22 @@ def create_cancel():
cancel1 = node.call(create_cancel)
self.assertIsNotNone(cancel1.packet)

self._logger.debug(u"!!!! second time")
self.assertRaises(RuntimeError, create_cancel)

@skip("Will change this test to a meaningful one")
def test_node_resolve_cancel_twice(self):
"""
Make sure that in the event of receiving two cancel messages from the same member, both will be stored,
and in case of receiving a lower one, that we will send the higher one back to the sender.
If we receive two cancel messages canceling the same message, the higher one should be stored as the
latest cancel messages.
MM gives NODE permission to cancel, NODE generates a message and then cancels it twice.
Both messages should be kept and the lowest one should be canceled.
The latest cancel message is the one that has the highest packet binary.
In this test, we first same the original message C0, then we send the higher cancel message C2,
and finally the lower cancel message C1.
The receiver first updates its database with C2 as the latest cancel message of C0.
When it receives C1, it will recognise that it has C2 > C1, and it sends C2 to the sender
to inform him with the latest cancel message of C0.
"""
node, other = self.create_nodes(2)
node.send_identity(other)
Expand All @@ -108,12 +111,9 @@ def test_node_resolve_cancel_twice(self):
cancel1 = node.create_cancel_own(message, 11)
cancel2 = node.create_cancel_own(message, 12)
low_message, high_message = sorted([cancel1, cancel2], key=lambda msg: msg.packet)
self._logger.debug("!!! give message")
other.give_message(message, node)
self._logger.debug("!!! give low_message")
other.give_message(low_message, node)
self._logger.debug("!!! give high_message")
other.give_message(high_message, node)
other.give_message(low_message, node)
# OTHER should send the first message back when receiving
# the second one (its "higher" than the one just received)
cancel_packets = list()
Expand All @@ -136,17 +136,16 @@ def fetch_all_messages():
self._logger.debug("_______ %s", row)
other.call(fetch_all_messages)

self._logger.debug("%d", len(low_message.packet))
self._logger.debug("%d", len(high_message.packet))

self.assertEqual(len(cancel_packets), len([low_message.packet]))
self.assertEqual(len(cancel_packets), len([high_message.packet]))

# NODE should have both messages on the database and the lowest one should be undone by the highest.
# NODE should have both messages on the database and the lowest one should be canceled by the highest.
messages = other.fetch_messages((u"dispersy-cancel-own",))
self.assertEquals(len(messages), 2)
other.assert_is_done(low_message)
other.assert_is_canceled(high_message)
other.assert_is_canceled(high_message, canceled_by=low_message)
other.assert_is_canceled(message, canceled_by=low_message)
other.assert_is_stored(low_message)
other.assert_is_stored(high_message)
other.assert_is_canceled(message, canceled_by=high_message)

def test_missing_message(self):
"""
Expand All @@ -170,7 +169,7 @@ def test_missing_message(self):

# receive the dispersy-missing-message messages
global_times = [message.distribution.global_time for message in messages]
global_time_requests = []
global_time_requests = list()

for _, message in node.receive_messages(names=[u"dispersy-missing-message"]):
self.assertEqual(message.payload.member.public_key, node.my_member.public_key)
Expand All @@ -181,14 +180,14 @@ def test_missing_message(self):
# give all 'delayed' messages
other.give_messages(messages, node)

# check that they are in the database and ARE undone
# check that they are in the database and ARE canceled
other.assert_is_canceled(messages=messages)
other.assert_is_stored(messages=cancels)

def test_revoke_causing_cancel(self):
"""
SELF gives NODE permission to cancel, OTHER created a message, NODE cancels the message, SELF
revokes the cancel permission AFTER the message was canceled -> the message is not re-done.
SELF gives NODE permission to cancel, OTHER created a message M1, NODE cancels the message with C1, SELF
revokes the cancel permission AFTER the message was canceled -> the message is not resumed.
"""
node, other = self.create_nodes(2)
node.send_identity(other)
Expand All @@ -207,11 +206,11 @@ def test_revoke_causing_cancel(self):
# NODE cancels the message
cancel = node.create_cancel_other(message, message.distribution.global_time + 1)
other.give_message(cancel, node)
other.assert_is_canceled(message)
other.assert_is_canceled(message, canceled_by=cancel)
other.assert_is_stored(cancel)

# SELF revoke cancel permission from NODE
revoke = self._mm.create_revoke([(node.my_member, self._community.get_meta_message(u"full-sync-text"),
u"cancel")])
other.give_message(revoke, self._mm)
other.assert_is_canceled(message)
other.assert_is_canceled(message, canceled_by=cancel)

0 comments on commit 162c809

Please sign in to comment.