MCPcopy Create free account
hub / github.com/apple/axlearn / test_ping

Method test_ping

axlearn/common/liveness_monitor_test.py:20–80  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

18 """Tests TfSummaryMonitor."""
19
20 def test_ping(self):
21 # Note: test runs in a temporary cwd. See TestWithTemporaryCWD.
22 os.makedirs("train_train", exist_ok=True)
23 writer = tf.summary.create_file_writer(os.path.join(os.getcwd(), "train_train"))
24
25 # Construct the monitor.
26 cfg = TfSummaryMonitor.default_config().set(
27 summary_dir=self._temp_root.name,
28 max_timeout_seconds=5,
29 max_start_seconds=1,
30 )
31 monitor: LivenessMonitor = cfg.instantiate()
32
33 # Pinging before monitor starts raises.
34 self.assertFalse(monitor.started())
35 with self.assertRaisesRegex(RuntimeError, "started"):
36 monitor.ping()
37
38 # Set initial timestamp.
39 monitor.reset()
40 self.assertTrue(monitor.started())
41
42 # Initial timestamp.
43 t0 = monitor._latest
44 # Within grace period of t0.
45 t0_grace_period = t0 + cfg.max_start_seconds
46 # Within one timeout window of t0.
47 t0_timeout = t0 + cfg.max_timeout_seconds
48
49 # Pinging within grace period always returns True.
50 with mock.patch("time.time", mock.MagicMock(return_value=t0_grace_period)):
51 self.assertTrue(monitor.ping())
52
53 # Pinging outside grace period succeeds if we're within timeout window.
54 with mock.patch("time.time", mock.MagicMock(return_value=t0_timeout)):
55 self.assertAlmostEqual(monitor._latest, t0)
56 self.assertTrue(monitor.ping())
57
58 # Pinging outside grace period + timeout fails.
59 with mock.patch("time.time", mock.MagicMock(return_value=t0_timeout + 1)):
60 self.assertAlmostEqual(monitor._latest, t0)
61 self.assertFalse(monitor.ping())
62
63 # Try creating some fake summaries.
64 # Note: tf.summary does not seem to use time.time, so mocking it doesn't work.
65 time.sleep(1)
66 with writer.as_default():
67 tf.summary.scalar("test", 0.1, step=123)
68 self.assertTrue(monitor.ping())
69 # Test that latest time is updated.
70 self.assertGreater(monitor._latest, t0)
71
72 # Test resetting.
73 with mock.patch("time.time", mock.MagicMock(return_value=t0 + 100)):
74 self.assertFalse(monitor.ping())
75
76 # Test that resetting the grace period succeeds.
77 monitor.reset()

Callers

nothing calls this directly

Calls 9

joinMethod · 0.80
as_defaultMethod · 0.80
setMethod · 0.45
default_configMethod · 0.45
instantiateMethod · 0.45
startedMethod · 0.45
pingMethod · 0.45
resetMethod · 0.45
patchMethod · 0.45

Tested by

no test coverage detected