@@ -125,6 +125,23 @@ def home_owned(home, hold=0.0): |
125 | 125 | PROBING_LIMIT = 300 # seconds: a probing phase that has not ended by then fails its test instead of hanging it |
126 | 126 |
|
127 | 127 |
|
| 128 | +def kit_looks_for_the_launcher(test): |
| 129 | + """[(monotonic time a look began, whether it found the launcher)]: each of the kit's handover looks that found |
| 130 | + the serving lock held (handoff.launcher_serving), recorded as it ran.""" |
| 131 | + from golive import handoff |
| 132 | + looks, real = [], handoff.launcher_serving |
| 133 | + |
| 134 | + def spy(home, svc): |
| 135 | + began = time.monotonic() |
| 136 | + found = real(home, svc) |
| 137 | + looks.append((began, found)) |
| 138 | + return found |
| 139 | + patcher = mock.patch.object(handoff, 'launcher_serving', spy) |
| 140 | + patcher.start() |
| 141 | + test.addCleanup(patcher.stop) |
| 142 | + return looks |
| 143 | + |
| 144 | + |
128 | 145 | def kit_meets_probes(test): |
129 | 146 | """An Event set once the kit's handover poll has found the serving lock held by another process than this |
130 | 147 | one (an SSH probe: this process is the fake launcher), so a probing phase surely tested what it is about.""" |
@@ -681,7 +698,8 @@ def test_a_probe_loop_on_the_serving_lock_is_not_the_launcher(self): |
681 | 698 | g = self.h.Gap(self.m.home) |
682 | 699 | g.adopt(coldcopy.hold_owner_lock(userdata, 0)) |
683 | 700 | met = kit_meets_probes(self) |
684 | | - stop, probing, probes, free = threading.Event(), threading.Event(), [], [] |
| 701 | + looks = kit_looks_for_the_launcher(self) |
| 702 | + stop, probing, probes, free, claimed = threading.Event(), threading.Event(), [], [], [] |
685 | 703 |
|
686 | 704 | def ssh_probes(): |
687 | 705 | try: |
@@ -701,21 +719,84 @@ def start(): # `t3 update`: it installs while launches probe, then its launcher |
701 | 719 | t.join() |
702 | 720 | launcher = sqlite3.connect(serving, isolation_level=None, timeout=1) |
703 | 721 | launcher.execute('BEGIN EXCLUSIVE') |
704 | | - claimed = time.monotonic() |
705 | | - for _ in range(300): |
706 | | - if not held(owner): |
707 | | - break |
| 722 | + claimed.append(time.monotonic()) |
| 723 | + end = claimed[0] + PROBING_LIMIT |
| 724 | + while held(owner) and time.monotonic() < end: |
708 | 725 | time.sleep(0.005) |
709 | | - free.append(time.monotonic() - claimed) |
| 726 | + free.append(not held(owner)) |
710 | 727 | launcher.close() |
711 | 728 | t = threading.Thread(target=ssh_probes, daemon=True) |
712 | 729 | t.start() |
713 | 730 | g.starting(start, NEW, self.service) |
714 | 731 | self.assertEqual(len(free), 1, 'the kit let go of the owner lock while only SSH probes held the serving lock') |
| 732 | + self.assertTrue(free[0], 'the kit kept the owner lock after the launcher claimed the serving lock') |
715 | 733 | self.assertGreaterEqual(len(probes), PROBES) |
716 | 734 | self.assertTrue(met.is_set(), 'the kit never met an SSH probe of the serving lock') |
717 | | - self.assertLess(free[0], 0.25) # within its server's 250 ms wait for the owner lock |
| 735 | + # #534: let go at the kit's first look after the claim, counted in looks, not in wall-clock time (a slow |
| 736 | + # runner stretches both alike): every look that began after the claim found the launcher, and there was one |
| 737 | + self.assertEqual([found for at, found in looks if at >= claimed[0]], [True]) |
718 | 738 | self.assertEqual(g.handover, 'launcher') |
| 739 | + self.assertIsInstance(g.handover_ms, int) |
| 740 | + |
| 741 | + def test_the_handover_delay_is_measured_and_journalled(self): |
| 742 | + """#534: the verify step journals how long the kit kept the owner lock after the launcher's claim (an upper |
| 743 | + bound), and names a handover that reached the 250 ms its server waits for the owner lock.""" |
| 744 | + userdata = self.m.path('home/userdata') |
| 745 | + os.makedirs(userdata) |
| 746 | + serving = self.m.path('home/runtime/service-serving.lock') |
| 747 | + owner = os.path.join(userdata, 'server-owner.sqlite') |
| 748 | + claims = [] |
| 749 | + |
| 750 | + def launcher(): # claims the serving lock and keeps it until the kit let go of the owner lock |
| 751 | + c = sqlite3.connect(serving, isolation_level=None, check_same_thread=False) |
| 752 | + c.execute('BEGIN EXCLUSIVE') |
| 753 | + claims.append(c) |
| 754 | + # (seconds a look that found the launcher takes, the server's wait in ms): the lateness is fixed by the wait, |
| 755 | + # never by how fast this runner is: 0.3 s is surely past 250 ms, and no runner reaches a wait of 10**9 ms |
| 756 | + for slow, wait, late in ((0.0, 10**9, False), (0.3, self.h.SERVER_LOCK_WAIT_MS, True)): |
| 757 | + real = self.h.launcher_serving |
| 758 | + |
| 759 | + def seen(home, svc, slow=slow, real=real): # a look stretched by load, after the claim |
| 760 | + found = real(home, svc) |
| 761 | + if found: |
| 762 | + time.sleep(slow) |
| 763 | + return found |
| 764 | + g = self.h.open_gap(self.m.home) |
| 765 | + g.adopt(coldcopy.hold_owner_lock(userdata, 0)) |
| 766 | + with mock.patch.object(self.h, 'launcher_serving', seen), \ |
| 767 | + mock.patch.object(self.h, 'SERVER_LOCK_WAIT_MS', wait): |
| 768 | + g.starting(launcher, NEW, self.service) |
| 769 | + self.assertFalse(held(owner)) |
| 770 | + facts = self.h.served(self.m.home) |
| 771 | + self.assertEqual(facts['handover'], 'launcher') |
| 772 | + self.assertIsInstance(facts['handoverMs'], int) |
| 773 | + self.assertGreaterEqual(facts['handoverMs'], slow * 1000) # an upper bound: it covers the slow look |
| 774 | + self.assertEqual('handoverLate' in facts, late, facts) |
| 775 | + if late: |
| 776 | + self.assertIn(f'up to {facts["handoverMs"]} ms after the launcher took its serving lock', |
| 777 | + facts['handoverLate']) |
| 778 | + claims.pop().close() |
| 779 | + g = self.h.open_gap(self.m.home) # no launcher: nothing to measure |
| 780 | + g.adopt(coldcopy.hold_owner_lock(userdata, 0)) |
| 781 | + with mock.patch.object(self.h, 'HANDOVER_WAIT', 0.05): |
| 782 | + g.starting(lambda: None, NEW, self.service) |
| 783 | + self.assertIsNone(g.handover_ms) # a run again: the last handover's delay is not carried over |
| 784 | + facts = self.h.served(self.m.home) |
| 785 | + self.assertEqual(facts['handover'], 'timeout') |
| 786 | + self.assertNotIn('handoverMs', facts) |
| 787 | + g = self.h.open_gap(self.m.home) # a start that failed half-way: nothing to measure |
| 788 | + g.adopt(coldcopy.hold_owner_lock(userdata, 0)) |
| 789 | + g.handover_ms = 7 # left by an earlier start of this gap |
| 790 | + |
| 791 | + def fails(): |
| 792 | + raise OSError('the start failed') |
| 793 | + with self.assertRaises(OSError): |
| 794 | + g.starting(fails, NEW, self.service) |
| 795 | + self.assertFalse(held(owner)) |
| 796 | + facts = self.h.served(self.m.home) |
| 797 | + self.assertEqual(facts['handover'], 'start failed') |
| 798 | + self.assertNotIn('handoverMs', facts) |
| 799 | + self.assertEqual(self.h.served(self.m.home), {'marker': None}) # no gap open: nothing measured |
719 | 800 |
|
720 | 801 | def test_only_the_service_s_main_process_holding_the_serving_lock_is_the_launcher(self): |
721 | 802 | """However long another process holds the serving lock, the kit keeps the owner lock; it lets go at once |
|