|
| 1 | +from oslo_log import log as logging |
| 2 | +from oslo_config import cfg |
| 3 | +from neutron.tests import base |
| 4 | + |
| 5 | +LOG: logging.KeywordArgumentAdapter = logging.getLogger(__name__) |
| 6 | + |
| 7 | + |
| 8 | +def set_logging_levels(): |
| 9 | + cfg.CONF.set_override("default_log_levels", [ |
| 10 | + 'networking_nsxv3.common.synchronization=WARNING', |
| 11 | + ]) |
| 12 | + |
| 13 | + |
| 14 | +class TestAgentRealizerScheduling(base.BaseTestCase): |
| 15 | + def setUp(self): |
| 16 | + super().setUp() |
| 17 | + |
| 18 | + set_logging_levels() |
| 19 | + logging.setup(cfg.CONF, "demo") |
| 20 | + |
| 21 | + |
| 22 | + def test_rerunner_simple(self): |
| 23 | + from networking_nsxv3.common.synchronization import JobRerunner, Runnable |
| 24 | + |
| 25 | + rerunner = JobRerunner() |
| 26 | + |
| 27 | + def nop(item): |
| 28 | + LOG.debug("nop(%s) called", item) |
| 29 | + |
| 30 | + # add some jobs |
| 31 | + for some_id in range(100): |
| 32 | + job = Runnable(str(some_id), nop) |
| 33 | + ret = rerunner.add_job(job) |
| 34 | + self.assertTrue(ret) |
| 35 | + |
| 36 | + # mark them all done -- note that because |
| 37 | + # if and fn-name are the same, this should match, |
| 38 | + # although creating new jobs |
| 39 | + for some_id in range(100): |
| 40 | + job = Runnable(str(some_id), nop) |
| 41 | + rerunner.job_done(job) |
| 42 | + |
| 43 | + # no job was added twice, so no job should be re-executed |
| 44 | + self.assertEqual(rerunner.pop(), None) |
| 45 | + |
| 46 | + |
| 47 | + def test_rerunner_rerun(self): |
| 48 | + from networking_nsxv3.common.synchronization import JobRerunner, Runnable |
| 49 | + |
| 50 | + rerunner = JobRerunner() |
| 51 | + |
| 52 | + def nop(item): |
| 53 | + # should never happen |
| 54 | + LOG.error("nop(%s) called", item) |
| 55 | + |
| 56 | + # add some jobs |
| 57 | + for some_id in range(100): |
| 58 | + job = Runnable(str(some_id), nop) |
| 59 | + ret = rerunner.add_job(job) |
| 60 | + self.assertTrue(ret) |
| 61 | + |
| 62 | + # add them again, twice |
| 63 | + for some_id in range(100): |
| 64 | + job = Runnable(str(some_id), nop) |
| 65 | + ret = rerunner.add_job(job) |
| 66 | + self.assertFalse(ret) |
| 67 | + ret = rerunner.add_job(job) |
| 68 | + self.assertFalse(ret) |
| 69 | + |
| 70 | + # mark them all done, then expect them |
| 71 | + # all to be returned once in pop, |
| 72 | + # although added twice again above |
| 73 | + for some_id in range(100): |
| 74 | + job = Runnable(str(some_id), nop) |
| 75 | + rerunner.job_done(job) |
| 76 | + |
| 77 | + alljobs = [] |
| 78 | + while job := rerunner.pop(): |
| 79 | + alljobs.append(job) |
| 80 | + |
| 81 | + # each job should be returned once |
| 82 | + self.assertEqual(len(alljobs), 100) |
| 83 | + |
| 84 | + # no job should have the same hash |
| 85 | + self.assertEqual(len(set(alljobs)), len(alljobs)) |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
0 commit comments