def test_basic_operations(self): c = CounterCell() c.inc(2) self.assertEqual(c.get_cumulative(), 2) c.dec(10) self.assertEqual(c.get_cumulative(), -8) c.dec() self.assertEqual(c.get_cumulative(), -9) c.inc() self.assertEqual(c.get_cumulative(), -8)
def test_parallel_access(self): # We create NUM_THREADS threads that concurrently modify the counter. threads = [] c = CounterCell() for _ in range(TestCounterCell.NUM_THREADS): t = threading.Thread(target=TestCounterCell._modify_counter, args=(c,)) threads.append(t) t.start() for t in threads: t.join() total = (self.NUM_ITERATIONS * (self.NUM_ITERATIONS-1)/2 * self.NUM_THREADS) self.assertEqual(c.get_cumulative(), total)
def test_parallel_access(self): # We create NUM_THREADS threads that concurrently modify the counter. threads = [] c = CounterCell() for _ in range(TestCounterCell.NUM_THREADS): t = threading.Thread(target=TestCounterCell._modify_counter, args=(c, )) threads.append(t) t.start() for t in threads: t.join() total = (self.NUM_ITERATIONS * (self.NUM_ITERATIONS - 1) // 2 * self.NUM_THREADS) self.assertEqual(c.get_cumulative(), total)