def run(): channels = { 'c1': goless.chan(0), 'c2': goless.chan(0), 'c3': goless.chan(0) } workers = [make_worker(logme, channels['c1'], channels['c2']), make_worker(logme, channels['c2'], channels['c3'])] logging.info("run: defining send_messages...") print("run: defining send_messages...") def send_messages(): for ii in range(10): print("sending val={} into channel['c1']={}", val, channels['c1']) val = "val:{}".format(ii) channels['c1'].send(val) res = goless.go(send_messages) print("called goless.go(send_messages) => {}".format(res)) while True: cases = [goless.rcase(channels['c3']), goless.dcase()] case, val = goless.select(cases) if case == cases[1]: print("hit default, perhaps things are closed? case={} val={}".format(case, val)) break print("case={}; c3 => {}".format(case, val)) print("closing channel c3") channels['c3'].close() print("exiting")
def test_select_chooses_ready_selection(self): readychan = goless.chan(1) notreadychan = goless.chan(1) readychan.send(3) cases = [goless.rcase(notreadychan), goless.rcase(readychan), goless.dcase()] result, val = goless.select(cases) self.assertIs(result, cases[1]) self.assertEqual(val, 3)
def test_case_switch(self): chan = goless.chan() cases = [goless.rcase(chan), goless.scase(chan, 1), goless.dcase()] chosen, value = goless.select(cases) if chosen is cases[0]: print('Received %s' % value) elif chosen is cases[1]: assert value is None print('Sent.') else: assert chosen is cases[2], chosen print('Default...')
def test_select_uses_default(self): cases = [goless.rcase(self.chan1), goless.dcase()] result, val = goless.select(cases) self.assertIs(result, cases[1]) self.assertIsNone(val)
def test_raises_if_multiple_default_cases(self): with self.assertRaises(AssertionError): goless.select([goless.dcase(), goless.dcase()])