u"" cleanup on test_server.py

This commit is contained in:
laharah 2016-06-04 13:27:28 -07:00
parent 2dd76f57c0
commit bb970dafc0

View File

@ -16,18 +16,18 @@ from ..server.database import get_db
class Server(ServerBase, unittest.TestCase):
def test_apps(self):
app1 = self._rendezvous.get_app(u"appid1")
self.assertIdentical(app1, self._rendezvous.get_app(u"appid1"))
app2 = self._rendezvous.get_app(u"appid2")
app1 = self._rendezvous.get_app("appid1")
self.assertIdentical(app1, self._rendezvous.get_app("appid1"))
app2 = self._rendezvous.get_app("appid2")
self.assertNotIdentical(app1, app2)
def test_nameplate_allocation(self):
app = self._rendezvous.get_app(u"appid")
app = self._rendezvous.get_app("appid")
nids = set()
# this takes a second, and claims all the short-numbered nameplates
def add():
nameplate_id = app.allocate_nameplate(u"side1", 0)
self.assertEqual(type(nameplate_id), type(u""))
nameplate_id = app.allocate_nameplate("side1", 0)
self.assertEqual(type(nameplate_id), type(""))
nid = int(nameplate_id)
nids.add(nid)
for i in range(9): add()
@ -53,41 +53,41 @@ class Server(ServerBase, unittest.TestCase):
(nameplate_id,)).fetchone()
def test_nameplate(self):
app = self._rendezvous.get_app(u"appid")
nameplate_id = app.allocate_nameplate(u"side1", 0)
self.assertEqual(type(nameplate_id), type(u""))
app = self._rendezvous.get_app("appid")
nameplate_id = app.allocate_nameplate("side1", 0)
self.assertEqual(type(nameplate_id), type(""))
nid = int(nameplate_id)
self.assert_(0 < nid < 10, nid)
self.assertEqual(app.get_nameplate_ids(), set([nameplate_id]))
# allocate also does a claim
row = self._nameplate(app, nameplate_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], None)
self.assertEqual(row["crowded"], False)
self.assertEqual(row["started"], 0)
self.assertEqual(row["second"], None)
mailbox_id = app.claim_nameplate(nameplate_id, u"side1", 1)
self.assertEqual(type(mailbox_id), type(u""))
mailbox_id = app.claim_nameplate(nameplate_id, "side1", 1)
self.assertEqual(type(mailbox_id), type(""))
# duplicate claims by the same side are combined
row = self._nameplate(app, nameplate_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], None)
mailbox_id2 = app.claim_nameplate(nameplate_id, u"side1", 2)
mailbox_id2 = app.claim_nameplate(nameplate_id, "side1", 2)
self.assertEqual(mailbox_id, mailbox_id2)
row = self._nameplate(app, nameplate_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], None)
self.assertEqual(row["started"], 0)
self.assertEqual(row["second"], None)
# claim by the second side is new
mailbox_id3 = app.claim_nameplate(nameplate_id, u"side2", 3)
mailbox_id3 = app.claim_nameplate(nameplate_id, "side2", 3)
self.assertEqual(mailbox_id, mailbox_id3)
row = self._nameplate(app, nameplate_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side2"], u"side2")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], "side2")
self.assertEqual(row["crowded"], False)
self.assertEqual(row["started"], 0)
self.assertEqual(row["second"], 3)
@ -95,43 +95,43 @@ class Server(ServerBase, unittest.TestCase):
# a third claim marks the nameplate as "crowded", but leaves the two
# existing claims alone
self.assertRaises(rendezvous.CrowdedError,
app.claim_nameplate, nameplate_id, u"side3", 0)
app.claim_nameplate, nameplate_id, "side3", 0)
row = self._nameplate(app, nameplate_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side2"], u"side2")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], "side2")
self.assertEqual(row["crowded"], True)
# releasing a non-existent nameplate is ignored
app.release_nameplate(nameplate_id+u"not", u"side4", 0)
app.release_nameplate(nameplate_id+"not", "side4", 0)
# releasing a side that never claimed the nameplate is ignored
app.release_nameplate(nameplate_id, u"side4", 0)
app.release_nameplate(nameplate_id, "side4", 0)
row = self._nameplate(app, nameplate_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side2"], u"side2")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], "side2")
# releasing one side leaves the second claim
app.release_nameplate(nameplate_id, u"side1", 5)
app.release_nameplate(nameplate_id, "side1", 5)
row = self._nameplate(app, nameplate_id)
self.assertEqual(row["side1"], u"side2")
self.assertEqual(row["side1"], "side2")
self.assertEqual(row["side2"], None)
# releasing one side multiple times is ignored
app.release_nameplate(nameplate_id, u"side1", 5)
app.release_nameplate(nameplate_id, "side1", 5)
row = self._nameplate(app, nameplate_id)
self.assertEqual(row["side1"], u"side2")
self.assertEqual(row["side1"], "side2")
self.assertEqual(row["side2"], None)
# releasing the second side frees the nameplate, and adds usage
app.release_nameplate(nameplate_id, u"side2", 6)
app.release_nameplate(nameplate_id, "side2", 6)
row = self._nameplate(app, nameplate_id)
self.assertEqual(row, None)
usage = app._db.execute("SELECT * FROM `nameplate_usage`").fetchone()
self.assertEqual(usage["app_id"], u"appid")
self.assertEqual(usage["app_id"], "appid")
self.assertEqual(usage["started"], 0)
self.assertEqual(usage["waiting_time"], 3)
self.assertEqual(usage["total_time"], 6)
self.assertEqual(usage["result"], u"crowded")
self.assertEqual(usage["result"], "crowded")
def _mailbox(self, app, mailbox_id):
@ -140,12 +140,12 @@ class Server(ServerBase, unittest.TestCase):
(mailbox_id,)).fetchone()
def test_mailbox(self):
app = self._rendezvous.get_app(u"appid")
mailbox_id = u"mid"
m1 = app.open_mailbox(mailbox_id, u"side1", 0)
app = self._rendezvous.get_app("appid")
mailbox_id = "mid"
m1 = app.open_mailbox(mailbox_id, "side1", 0)
row = self._mailbox(app, mailbox_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], None)
self.assertEqual(row["crowded"], False)
self.assertEqual(row["started"], 0)
@ -153,55 +153,55 @@ class Server(ServerBase, unittest.TestCase):
# opening the same mailbox twice, by the same side, gets the same
# object
self.assertIdentical(m1, app.open_mailbox(mailbox_id, u"side1", 1))
self.assertIdentical(m1, app.open_mailbox(mailbox_id, "side1", 1))
row = self._mailbox(app, mailbox_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], None)
self.assertEqual(row["crowded"], False)
self.assertEqual(row["started"], 0)
self.assertEqual(row["second"], None)
# opening a second side gets the same object, and adds a new claim
self.assertIdentical(m1, app.open_mailbox(mailbox_id, u"side2", 2))
self.assertIdentical(m1, app.open_mailbox(mailbox_id, "side2", 2))
row = self._mailbox(app, mailbox_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side2"], u"side2")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], "side2")
self.assertEqual(row["crowded"], False)
self.assertEqual(row["started"], 0)
self.assertEqual(row["second"], 2)
# a third open marks it as crowded
self.assertRaises(rendezvous.CrowdedError,
app.open_mailbox, mailbox_id, u"side3", 3)
app.open_mailbox, mailbox_id, "side3", 3)
row = self._mailbox(app, mailbox_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side2"], u"side2")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], "side2")
self.assertEqual(row["crowded"], True)
self.assertEqual(row["started"], 0)
self.assertEqual(row["second"], 2)
# closing a side that never claimed the mailbox is ignored
m1.close(u"side4", u"mood", 4)
m1.close("side4", "mood", 4)
row = self._mailbox(app, mailbox_id)
self.assertEqual(row["side1"], u"side1")
self.assertEqual(row["side2"], u"side2")
self.assertEqual(row["side1"], "side1")
self.assertEqual(row["side2"], "side2")
self.assertEqual(row["crowded"], True)
self.assertEqual(row["started"], 0)
self.assertEqual(row["second"], 2)
# closing one side leaves the second claim
m1.close(u"side1", u"mood", 5)
m1.close("side1", "mood", 5)
row = self._mailbox(app, mailbox_id)
self.assertEqual(row["side1"], u"side2")
self.assertEqual(row["side1"], "side2")
self.assertEqual(row["side2"], None)
self.assertEqual(row["crowded"], True)
self.assertEqual(row["started"], 0)
self.assertEqual(row["second"], 2)
# closing one side multiple is ignored
m1.close(u"side1", u"mood", 6)
m1.close("side1", "mood", 6)
row = self._mailbox(app, mailbox_id)
self.assertEqual(row["side1"], u"side2")
self.assertEqual(row["side1"], "side2")
self.assertEqual(row["side2"], None)
self.assertEqual(row["crowded"], True)
self.assertEqual(row["started"], 0)
@ -211,17 +211,17 @@ class Server(ServerBase, unittest.TestCase):
m1.add_listener("handle1", l1.append, stop1_f)
# closing the second side frees the mailbox, and adds usage
m1.close(u"side2", u"mood", 7)
m1.close("side2", "mood", 7)
self.assertEqual(stop1, [True])
row = self._mailbox(app, mailbox_id)
self.assertEqual(row, None)
usage = app._db.execute("SELECT * FROM `mailbox_usage`").fetchone()
self.assertEqual(usage["app_id"], u"appid")
self.assertEqual(usage["app_id"], "appid")
self.assertEqual(usage["started"], 0)
self.assertEqual(usage["waiting_time"], 2)
self.assertEqual(usage["total_time"], 7)
self.assertEqual(usage["result"], u"crowded")
self.assertEqual(usage["result"], "crowded")
def _messages(self, app):
c = app._db.execute("SELECT * FROM `messages`"
@ -229,85 +229,85 @@ class Server(ServerBase, unittest.TestCase):
return c.fetchall()
def test_messages(self):
app = self._rendezvous.get_app(u"appid")
mailbox_id = u"mid"
m1 = app.open_mailbox(mailbox_id, u"side1", 0)
m1.add_message(SidedMessage(side=u"side1", phase=u"phase",
body=u"body", server_rx=1,
msg_id=u"msgid"))
app = self._rendezvous.get_app("appid")
mailbox_id = "mid"
m1 = app.open_mailbox(mailbox_id, "side1", 0)
m1.add_message(SidedMessage(side="side1", phase="phase",
body="body", server_rx=1,
msg_id="msgid"))
msgs = self._messages(app)
self.assertEqual(len(msgs), 1)
self.assertEqual(msgs[0]["body"], u"body")
self.assertEqual(msgs[0]["body"], "body")
l1 = []; stop1 = []; stop1_f = lambda: stop1.append(True)
l2 = []; stop2 = []; stop2_f = lambda: stop2.append(True)
old = m1.add_listener("handle1", l1.append, stop1_f)
self.assertEqual(len(old), 1)
self.assertEqual(old[0].side, u"side1")
self.assertEqual(old[0].body, u"body")
self.assertEqual(old[0].side, "side1")
self.assertEqual(old[0].body, "body")
m1.add_message(SidedMessage(side=u"side1", phase=u"phase2",
body=u"body2", server_rx=1,
msg_id=u"msgid"))
m1.add_message(SidedMessage(side="side1", phase="phase2",
body="body2", server_rx=1,
msg_id="msgid"))
self.assertEqual(len(l1), 1)
self.assertEqual(l1[0].body, u"body2")
self.assertEqual(l1[0].body, "body2")
old = m1.add_listener("handle2", l2.append, stop2_f)
self.assertEqual(len(old), 2)
m1.add_message(SidedMessage(side=u"side1", phase=u"phase3",
body=u"body3", server_rx=1,
msg_id=u"msgid"))
m1.add_message(SidedMessage(side="side1", phase="phase3",
body="body3", server_rx=1,
msg_id="msgid"))
self.assertEqual(len(l1), 2)
self.assertEqual(l1[-1].body, u"body3")
self.assertEqual(l1[-1].body, "body3")
self.assertEqual(len(l2), 1)
self.assertEqual(l2[-1].body, u"body3")
self.assertEqual(l2[-1].body, "body3")
m1.remove_listener("handle1")
m1.add_message(SidedMessage(side=u"side1", phase=u"phase4",
body=u"body4", server_rx=1,
msg_id=u"msgid"))
m1.add_message(SidedMessage(side="side1", phase="phase4",
body="body4", server_rx=1,
msg_id="msgid"))
self.assertEqual(len(l1), 2)
self.assertEqual(l1[-1].body, u"body3")
self.assertEqual(l1[-1].body, "body3")
self.assertEqual(len(l2), 2)
self.assertEqual(l2[-1].body, u"body4")
self.assertEqual(l2[-1].body, "body4")
m1._shutdown()
self.assertEqual(stop1, [])
self.assertEqual(stop2, [True])
# message adds are not idempotent: clients filter duplicates
m1.add_message(SidedMessage(side=u"side1", phase=u"phase",
body=u"body", server_rx=1,
msg_id=u"msgid"))
m1.add_message(SidedMessage(side="side1", phase="phase",
body="body", server_rx=1,
msg_id="msgid"))
msgs = self._messages(app)
self.assertEqual(len(msgs), 5)
self.assertEqual(msgs[-1]["body"], u"body")
self.assertEqual(msgs[-1]["body"], "body")
class Prune(unittest.TestCase):
def test_apps(self):
rv = rendezvous.Rendezvous(get_db(":memory:"), None, None)
app = rv.get_app(u"appid")
app.allocate_nameplate(u"side", 121)
app = rv.get_app("appid")
app.allocate_nameplate("side", 121)
app.prune = mock.Mock()
rv.prune(now=123, old=122)
self.assertEqual(app.prune.mock_calls, [mock.call(123, 122)])
def test_active(self):
rv = rendezvous.Rendezvous(get_db(":memory:"), None, None)
app = rv.get_app(u"appid1")
app = rv.get_app("appid1")
self.assertFalse(app.is_active())
mb = app.open_mailbox(u"mbid", u"side1", 0)
mb = app.open_mailbox("mbid", "side1", 0)
self.assertFalse(mb.is_active())
self.assertFalse(app.is_active())
mb.add_listener(u"handle", None, None)
mb.add_listener("handle", None, None)
self.assertTrue(mb.is_active())
self.assertTrue(app.is_active())
mb.remove_listener(u"handle")
mb.remove_listener("handle")
self.assertFalse(mb.is_active())
self.assertFalse(app.is_active())
@ -322,35 +322,35 @@ class Prune(unittest.TestCase):
new_mailboxes = set()
new_messages = set()
APPID = u"appid"
APPID = "appid"
app = rv.get_app(APPID)
# Exercise the first-vs-second newness tests. These nameplates have
# no mailbox.
app.claim_nameplate(u"np-1", u"side1", 1)
app.claim_nameplate(u"np-2", u"side1", 1)
app.claim_nameplate(u"np-2", u"side2", 2)
app.claim_nameplate(u"np-3", u"side1", 60)
new_nameplates.add(u"np-3")
app.claim_nameplate(u"np-4", u"side1", 1)
app.claim_nameplate(u"np-4", u"side2", 60)
new_nameplates.add(u"np-4")
app.claim_nameplate(u"np-5", u"side1", 60)
app.claim_nameplate(u"np-5", u"side2", 61)
new_nameplates.add(u"np-5")
app.claim_nameplate("np-1", "side1", 1)
app.claim_nameplate("np-2", "side1", 1)
app.claim_nameplate("np-2", "side2", 2)
app.claim_nameplate("np-3", "side1", 60)
new_nameplates.add("np-3")
app.claim_nameplate("np-4", "side1", 1)
app.claim_nameplate("np-4", "side2", 60)
new_nameplates.add("np-4")
app.claim_nameplate("np-5", "side1", 60)
app.claim_nameplate("np-5", "side2", 61)
new_nameplates.add("np-5")
# same for mailboxes
app.open_mailbox(u"mb-11", u"side1", 1)
app.open_mailbox(u"mb-12", u"side1", 1)
app.open_mailbox(u"mb-12", u"side2", 2)
app.open_mailbox(u"mb-13", u"side1", 60)
new_mailboxes.add(u"mb-13")
app.open_mailbox(u"mb-14", u"side1", 1)
app.open_mailbox(u"mb-14", u"side2", 60)
new_mailboxes.add(u"mb-14")
app.open_mailbox(u"mb-15", u"side1", 60)
app.open_mailbox(u"mb-15", u"side2", 61)
new_mailboxes.add(u"mb-15")
app.open_mailbox("mb-11", "side1", 1)
app.open_mailbox("mb-12", "side1", 1)
app.open_mailbox("mb-12", "side2", 2)
app.open_mailbox("mb-13", "side1", 60)
new_mailboxes.add("mb-13")
app.open_mailbox("mb-14", "side1", 1)
app.open_mailbox("mb-14", "side2", 60)
new_mailboxes.add("mb-14")
app.open_mailbox("mb-15", "side1", 60)
app.open_mailbox("mb-15", "side2", 61)
new_mailboxes.add("mb-15")
rv.prune(now=123, old=50)
@ -387,7 +387,7 @@ class Prune(unittest.TestCase):
db = get_db(":memory:")
rv = rendezvous.Rendezvous(db, None, 3600)
APPID = u"appid"
APPID = "appid"
app = rv.get_app(APPID)
# timestamps <=50 are "old", >=51 are "new"
@ -397,12 +397,12 @@ class Prune(unittest.TestCase):
mailbox_survives = False
messages_survive = False
mbid = u"mbid"
mbid = "mbid"
if nameplate is not None:
app.claim_nameplate(u"npid", u"side1", when[nameplate],
app.claim_nameplate("npid", "side1", when[nameplate],
_test_mailbox_id=mbid)
if mailbox is not None:
mb = app.open_mailbox(mbid, u"side1", when[mailbox])
mb = app.open_mailbox(mbid, "side1", when[mailbox])
else:
# We might want a Mailbox, because that's the easiest way to add
# a "messages" row, but we can't use app.open_mailbox() because
@ -415,8 +415,8 @@ class Prune(unittest.TestCase):
app._mailboxes[mbid] = mb
if messages is not None:
sm = SidedMessage(u"side1", u"phase", u"body", when[messages],
u"msgid")
sm = SidedMessage("side1", "phase", "body", when[messages],
"msgid")
mb.add_message(sm)
if has_listeners:
@ -500,7 +500,7 @@ class WSClient(websocket.WebSocketClientProtocol):
returnValue(m)
def strip_acks(self):
self.events = [e for e in self.events if e["type"] != u"ack"]
self.events = [e for e in self.events if e["type"] != "ack"]
def send(self, mtype, **kwargs):
kwargs["type"] = mtype
@ -630,126 +630,126 @@ class WebSocketAPI(ServerBase, unittest.TestCase):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"bind", appid=u"appid") # missing side=
c1.send("bind", appid="appid") # missing side=
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"bind requires 'side'")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "bind requires 'side'")
c1.send(u"bind", side=u"side") # missing appid=
c1.send("bind", side="side") # missing appid=
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"bind requires 'appid'")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "bind requires 'appid'")
c1.send(u"bind", appid=u"appid", side=u"side")
c1.send("bind", appid="appid", side="side")
yield c1.sync()
self.assertEqual(list(self._rendezvous._apps.keys()), [u"appid"])
self.assertEqual(list(self._rendezvous._apps.keys()), ["appid"])
c1.send(u"bind", appid=u"appid", side=u"side") # duplicate
c1.send("bind", appid="appid", side="side") # duplicate
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"already bound")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "already bound")
c1.send_notype(other="misc") # missing 'type'
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"missing 'type'")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "missing 'type'")
c1.send("___unknown") # unknown type
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"unknown type")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "unknown type")
c1.send("ping") # missing 'ping'
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"ping requires 'ping'")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "ping requires 'ping'")
@inlineCallbacks
def test_list(self):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"list") # too early, must bind first
c1.send("list") # too early, must bind first
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"must bind first")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "must bind first")
c1.send(u"bind", appid=u"appid", side=u"side")
c1.send(u"list")
c1.send("bind", appid="appid", side="side")
c1.send("list")
m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"nameplates")
self.assertEqual(m[u"nameplates"], [])
self.assertEqual(m["type"], "nameplates")
self.assertEqual(m["nameplates"], [])
app = self._rendezvous.get_app(u"appid")
nameplate_id1 = app.allocate_nameplate(u"side", 0)
app.claim_nameplate(u"np2", u"side", 0)
app = self._rendezvous.get_app("appid")
nameplate_id1 = app.allocate_nameplate("side", 0)
app.claim_nameplate("np2", "side", 0)
c1.send(u"list")
c1.send("list")
m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"nameplates")
self.assertEqual(m["type"], "nameplates")
nids = set()
for n in m[u"nameplates"]:
for n in m["nameplates"]:
self.assertEqual(type(n), dict)
self.assertEqual(list(n.keys()), [u"id"])
nids.add(n[u"id"])
self.assertEqual(nids, set([nameplate_id1, u"np2"]))
self.assertEqual(list(n.keys()), ["id"])
nids.add(n["id"])
self.assertEqual(nids, set([nameplate_id1, "np2"]))
@inlineCallbacks
def test_allocate(self):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"allocate") # too early, must bind first
c1.send("allocate") # too early, must bind first
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"must bind first")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "must bind first")
c1.send(u"bind", appid=u"appid", side=u"side")
app = self._rendezvous.get_app(u"appid")
c1.send(u"allocate")
c1.send("bind", appid="appid", side="side")
app = self._rendezvous.get_app("appid")
c1.send("allocate")
m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"allocated")
nameplate_id = m[u"nameplate"]
self.assertEqual(m["type"], "allocated")
nameplate_id = m["nameplate"]
nids = app.get_nameplate_ids()
self.assertEqual(len(nids), 1)
self.assertEqual(nameplate_id, list(nids)[0])
c1.send(u"allocate")
c1.send("allocate")
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"],
u"you already allocated one, don't be greedy")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"],
"you already allocated one, don't be greedy")
c1.send(u"claim", nameplate=nameplate_id) # allocate+claim is ok
c1.send("claim", nameplate=nameplate_id) # allocate+claim is ok
yield c1.sync()
row = app._db.execute("SELECT * FROM `nameplates`"
" WHERE `app_id`='appid' AND `id`=?",
(nameplate_id,)).fetchone()
self.assertEqual(row["side1"], u"side")
self.assertEqual(row["side1"], "side")
self.assertEqual(row["side2"], None)
@inlineCallbacks
def test_claim(self):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"bind", appid=u"appid", side=u"side")
app = self._rendezvous.get_app(u"appid")
c1.send("bind", appid="appid", side="side")
app = self._rendezvous.get_app("appid")
c1.send(u"claim") # missing nameplate=
c1.send("claim") # missing nameplate=
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"claim requires 'nameplate'")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "claim requires 'nameplate'")
c1.send(u"claim", nameplate=u"np1")
c1.send("claim", nameplate="np1")
m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"claimed")
mailbox_id = m[u"mailbox"]
self.assertEqual(type(mailbox_id), type(u""))
self.assertEqual(m["type"], "claimed")
mailbox_id = m["mailbox"]
self.assertEqual(type(mailbox_id), type(""))
nids = app.get_nameplate_ids()
self.assertEqual(len(nids), 1)
self.assertEqual(u"np1", list(nids)[0])
self.assertEqual("np1", list(nids)[0])
# claiming a nameplate will assign a random mailbox id, but won't
# create the mailbox itself
@ -761,212 +761,212 @@ class WebSocketAPI(ServerBase, unittest.TestCase):
def test_claim_crowded(self):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"bind", appid=u"appid", side=u"side")
app = self._rendezvous.get_app(u"appid")
c1.send("bind", appid="appid", side="side")
app = self._rendezvous.get_app("appid")
app.claim_nameplate(u"np1", u"side1", 0)
app.claim_nameplate(u"np1", u"side2", 0)
app.claim_nameplate("np1", "side1", 0)
app.claim_nameplate("np1", "side2", 0)
# the third claim will signal crowding
c1.send(u"claim", nameplate=u"np1")
c1.send("claim", nameplate="np1")
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"crowded")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "crowded")
@inlineCallbacks
def test_release(self):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"bind", appid=u"appid", side=u"side")
app = self._rendezvous.get_app(u"appid")
c1.send("bind", appid="appid", side="side")
app = self._rendezvous.get_app("appid")
app.claim_nameplate(u"np1", u"side2", 0)
app.claim_nameplate("np1", "side2", 0)
c1.send(u"release") # didn't do claim first
c1.send("release") # didn't do claim first
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"],
u"must claim a nameplate before releasing it")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"],
"must claim a nameplate before releasing it")
c1.send(u"claim", nameplate=u"np1")
c1.send("claim", nameplate="np1")
yield c1.next_non_ack()
c1.send(u"release")
c1.send("release")
m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"released")
self.assertEqual(m["type"], "released")
row = app._db.execute("SELECT * FROM `nameplates`"
" WHERE `app_id`='appid' AND `id`='np1'").fetchone()
self.assertEqual(row["side1"], u"side2")
self.assertEqual(row["side1"], "side2")
self.assertEqual(row["side2"], None)
c1.send(u"release") # no longer claimed
c1.send("release") # no longer claimed
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"],
u"must claim a nameplate before releasing it")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"],
"must claim a nameplate before releasing it")
@inlineCallbacks
def test_open(self):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"bind", appid=u"appid", side=u"side")
app = self._rendezvous.get_app(u"appid")
c1.send("bind", appid="appid", side="side")
app = self._rendezvous.get_app("appid")
c1.send(u"open") # missing mailbox=
c1.send("open") # missing mailbox=
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"open requires 'mailbox'")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "open requires 'mailbox'")
mb1 = app.open_mailbox(u"mb1", u"side2", 0)
mb1.add_message(SidedMessage(side=u"side2", phase=u"phase",
body=u"body", server_rx=0,
msg_id=u"msgid"))
mb1 = app.open_mailbox("mb1", "side2", 0)
mb1.add_message(SidedMessage(side="side2", phase="phase",
body="body", server_rx=0,
msg_id="msgid"))
c1.send(u"open", mailbox=u"mb1")
c1.send("open", mailbox="mb1")
m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"message")
self.assertEqual(m[u"body"], u"body")
self.assertEqual(m["type"], "message")
self.assertEqual(m["body"], "body")
mb1.add_message(SidedMessage(side=u"side2", phase=u"phase2",
body=u"body2", server_rx=0,
msg_id=u"msgid"))
mb1.add_message(SidedMessage(side="side2", phase="phase2",
body="body2", server_rx=0,
msg_id="msgid"))
m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"message")
self.assertEqual(m[u"body"], u"body2")
self.assertEqual(m["type"], "message")
self.assertEqual(m["body"], "body2")
c1.send(u"open", mailbox=u"mb1")
c1.send("open", mailbox="mb1")
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"you already have a mailbox open")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "you already have a mailbox open")
@inlineCallbacks
def test_add(self):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"bind", appid=u"appid", side=u"side")
app = self._rendezvous.get_app(u"appid")
mb1 = app.open_mailbox(u"mb1", u"side2", 0)
c1.send("bind", appid="appid", side="side")
app = self._rendezvous.get_app("appid")
mb1 = app.open_mailbox("mb1", "side2", 0)
l1 = []; stop1 = []; stop1_f = lambda: stop1.append(True)
mb1.add_listener("handle1", l1.append, stop1_f)
c1.send(u"add") # didn't open first
c1.send("add") # didn't open first
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"must open mailbox before adding")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "must open mailbox before adding")
c1.send(u"open", mailbox=u"mb1")
c1.send("open", mailbox="mb1")
c1.send(u"add", body=u"body") # missing phase=
c1.send("add", body="body") # missing phase=
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"missing 'phase'")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "missing 'phase'")
c1.send(u"add", phase=u"phase") # missing body=
c1.send("add", phase="phase") # missing body=
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"missing 'body'")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "missing 'body'")
c1.send(u"add", phase=u"phase", body=u"body")
c1.send("add", phase="phase", body="body")
m = yield c1.next_non_ack() # echoed back
self.assertEqual(m[u"type"], u"message")
self.assertEqual(m[u"body"], u"body")
self.assertEqual(m["type"], "message")
self.assertEqual(m["body"], "body")
self.assertEqual(len(l1), 1)
self.assertEqual(l1[0].body, u"body")
self.assertEqual(l1[0].body, "body")
@inlineCallbacks
def test_close(self):
c1 = yield self.make_client()
yield c1.next_non_ack()
c1.send(u"bind", appid=u"appid", side=u"side")
c1.send("bind", appid="appid", side="side")
c1.send(u"close", mood=u"mood") # must open first
c1.send("close", mood="mood") # must open first
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"must open mailbox before closing")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "must open mailbox before closing")
c1.send(u"open", mailbox=u"mb1")
c1.send(u"close", mood=u"mood")
c1.send("open", mailbox="mb1")
c1.send("close", mood="mood")
m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"closed")
self.assertEqual(m["type"], "closed")
c1.send(u"close", mood=u"mood") # already closed
c1.send("close", mood="mood") # already closed
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"must open mailbox before closing")
self.assertEqual(err["type"], "error")
self.assertEqual(err["error"], "must open mailbox before closing")
class Summary(unittest.TestCase):
def test_mailbox(self):
app = rendezvous.AppNamespace(None, None, False, None)
# starts at time 1, maybe gets second open at time 3, closes at 5
base_row = {u"started": 1, u"second": None,
u"first_mood": None, u"crowded": False}
base_row = {"started": 1, "second": None,
"first_mood": None, "crowded": False}
def summ(num_sides, second_mood=None, pruned=False, **kwargs):
row = base_row.copy()
row.update(kwargs)
return app._summarize_mailbox(row, num_sides, second_mood, 5,
pruned)
self.assertEqual(summ(1), Usage(1, None, 4, u"lonely"))
self.assertEqual(summ(1, u"lonely"), Usage(1, None, 4, u"lonely"))
self.assertEqual(summ(1, u"errory"), Usage(1, None, 4, u"errory"))
self.assertEqual(summ(1, crowded=True), Usage(1, None, 4, u"crowded"))
self.assertEqual(summ(1), Usage(1, None, 4, "lonely"))
self.assertEqual(summ(1, "lonely"), Usage(1, None, 4, "lonely"))
self.assertEqual(summ(1, "errory"), Usage(1, None, 4, "errory"))
self.assertEqual(summ(1, crowded=True), Usage(1, None, 4, "crowded"))
self.assertEqual(summ(2, first_mood=u"happy",
second=3, second_mood=u"happy"),
Usage(1, 2, 4, u"happy"))
self.assertEqual(summ(2, first_mood="happy",
second=3, second_mood="happy"),
Usage(1, 2, 4, "happy"))
self.assertEqual(summ(2, first_mood=u"errory",
second=3, second_mood=u"happy"),
Usage(1, 2, 4, u"errory"))
self.assertEqual(summ(2, first_mood="errory",
second=3, second_mood="happy"),
Usage(1, 2, 4, "errory"))
self.assertEqual(summ(2, first_mood=u"happy",
second=3, second_mood=u"errory"),
Usage(1, 2, 4, u"errory"))
self.assertEqual(summ(2, first_mood="happy",
second=3, second_mood="errory"),
Usage(1, 2, 4, "errory"))
self.assertEqual(summ(2, first_mood=u"scary",
second=3, second_mood=u"happy"),
Usage(1, 2, 4, u"scary"))
self.assertEqual(summ(2, first_mood="scary",
second=3, second_mood="happy"),
Usage(1, 2, 4, "scary"))
self.assertEqual(summ(2, first_mood=u"scary",
second=3, second_mood=u"errory"),
Usage(1, 2, 4, u"scary"))
self.assertEqual(summ(2, first_mood="scary",
second=3, second_mood="errory"),
Usage(1, 2, 4, "scary"))
self.assertEqual(summ(2, first_mood=u"happy", second=3, pruned=True),
Usage(1, 2, 4, u"pruney"))
self.assertEqual(summ(2, first_mood="happy", second=3, pruned=True),
Usage(1, 2, 4, "pruney"))
def test_nameplate(self):
a = rendezvous.AppNamespace(None, None, False, None)
# starts at time 1, maybe gets second open at time 3, closes at 5
base_row = {u"started": 1, u"second": None, u"crowded": False}
base_row = {"started": 1, "second": None, "crowded": False}
def summ(num_sides, pruned=False, **kwargs):
row = base_row.copy()
row.update(kwargs)
return a._summarize_nameplate_usage(row, 5, pruned)
self.assertEqual(summ(1), Usage(1, None, 4, u"lonely"))
self.assertEqual(summ(1, crowded=True), Usage(1, None, 4, u"crowded"))
self.assertEqual(summ(1), Usage(1, None, 4, "lonely"))
self.assertEqual(summ(1, crowded=True), Usage(1, None, 4, "crowded"))
self.assertEqual(summ(2, second=3), Usage(1, 2, 4, u"happy"))
self.assertEqual(summ(2, second=3), Usage(1, 2, 4, "happy"))
self.assertEqual(summ(2, second=3, pruned=True),
Usage(1, 2, 4, u"pruney"))
Usage(1, 2, 4, "pruney"))
def test_blur(self):
db = get_db(":memory:")
rv = rendezvous.Rendezvous(db, None, 3600)
APPID = u"appid"
APPID = "appid"
app = rv.get_app(APPID)
app.claim_nameplate(u"npid", u"side1", 10) # start time is 10
app.claim_nameplate("npid", "side1", 10) # start time is 10
rv.prune(now=123, old=50)
# start time should be rounded to top of the hour (blur_usage=3600)
row = db.execute("SELECT * FROM `nameplate_usage`").fetchone()
self.assertEqual(row["started"], 0)
app = rv.get_app(APPID)
app.open_mailbox(u"mbid", u"side1", 20) # start time is 20
app.open_mailbox("mbid", "side1", 20) # start time is 20
rv.prune(now=123, old=50)
row = db.execute("SELECT * FROM `mailbox_usage`").fetchone()
self.assertEqual(row["started"], 0)
@ -974,15 +974,15 @@ class Summary(unittest.TestCase):
def test_no_blur(self):
db = get_db(":memory:")
rv = rendezvous.Rendezvous(db, None, None)
APPID = u"appid"
APPID = "appid"
app = rv.get_app(APPID)
app.claim_nameplate(u"npid", u"side1", 10) # start time is 10
app.claim_nameplate("npid", "side1", 10) # start time is 10
rv.prune(now=123, old=50)
row = db.execute("SELECT * FROM `nameplate_usage`").fetchone()
self.assertEqual(row["started"], 10)
app = rv.get_app(APPID)
app.open_mailbox(u"mbid", u"side1", 20) # start time is 20
app.open_mailbox("mbid", "side1", 20) # start time is 20
rv.prune(now=123, old=50)
row = db.execute("SELECT * FROM `mailbox_usage`").fetchone()
self.assertEqual(row["started"], 20)