test line chooser sticky
parent
ac7b583f76
commit
c6b648195f
|
|
@ -0,0 +1,36 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import writer
|
||||||
|
|
||||||
|
class TestLineChooser(unittest.TestCase):
|
||||||
|
def test_latest_sticky(self):
|
||||||
|
stale_something = self.new_line(False, 1, 1)
|
||||||
|
stale_nothing = self.new_line(False, 1, None)
|
||||||
|
old_something = self.new_line(True, 2, 2)
|
||||||
|
old_nothing = self.new_line(True, 2, None)
|
||||||
|
new_something = self.new_line(True, 3, 3)
|
||||||
|
new_nothing = self.new_line(True, 3, None)
|
||||||
|
|
||||||
|
chooser = writer.LineChooserLatestSticky()
|
||||||
|
|
||||||
|
for name, c in ({
|
||||||
|
"nothing over stale": [new_nothing, stale_something],
|
||||||
|
"slightly old over nothing": [old_something, new_nothing],
|
||||||
|
"new and nonzero": [new_something, old_something],
|
||||||
|
"new over nothing": [new_something, old_nothing],
|
||||||
|
"new over stale nothing": [new_something, stale_nothing],
|
||||||
|
}).items():
|
||||||
|
self.assertEqual(
|
||||||
|
c[0],
|
||||||
|
chooser.choose(c[0], c[1]),
|
||||||
|
name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def new_line(self, is_recent, t, v):
|
||||||
|
result = writer.Line(v)
|
||||||
|
result.t = t
|
||||||
|
result.is_recent = lambda *args: is_recent
|
||||||
|
return result
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
|
|
@ -78,10 +78,10 @@ class Line:
|
||||||
return not self.v
|
return not self.v
|
||||||
|
|
||||||
def is_recent(self):
|
def is_recent(self):
|
||||||
return time.time() - self.t < 1
|
return time.time() - self.t < 5
|
||||||
|
|
||||||
class LineChooser:
|
class LineChooser:
|
||||||
def choose(a, b):
|
def choose(self, a, b):
|
||||||
if not a:
|
if not a:
|
||||||
return b
|
return b
|
||||||
if not b:
|
if not b:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue