Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
    def test_select_no_default_no_ready_blocks(self):
        chan1 = goless.chan()
        chan2 = goless.chan()
        a = []
        cases = [goless.rcase(chan2), goless.rcase(chan1)]

        def sel():
            a.append(goless.select(cases))
        be.run(sel)
        self.assertEqual(a, [])
        chan1.send(5)
        be.yield_()
        self.assertEqual(len(a), 1)
        chosen, val = a[0]
        self.assertEqual(chosen, cases[1])
        self.assertEqual(val, 5)
Esempio n. 4
0
    def test_select_no_default_no_ready_blocks(self):
        chan1 = goless.chan()
        chan2 = goless.chan()
        a = []
        cases = [goless.rcase(chan2), goless.rcase(chan1)]

        def sel():
            a.append(goless.select(cases))
        be.run(sel)
        self.assertEqual(a, [])
        chan1.send(5)
        be.yield_()
        self.assertEqual(len(a), 1)
        chosen, val = a[0]
        self.assertEqual(chosen, cases[1])
        self.assertEqual(val, 5)
Esempio n. 5
0
 def func1():
     print("{}[{}] waiting for val".format(name, params['val']))
     case, val = goless.select([goless.rcase(params['cin'])])
     print("{}[{}] got case={}, val={}".format(name, params['val'], case, val))
     time.sleep(params['sleep'])
     print("{}[{}] sending to cout...".format(name, params['val']))
     params['cout'].send("(" + name + ")=" + val + ";" + params['val'])
Esempio n. 6
0
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")
Esempio n. 7
0
 def worker():
     while True:
         case, val = goless.select([goless.rcase(cin)])
         if val is None:
             print("cin returned None, is it closed?")
             cout.close()
             return
         print("f1: case={}, val={}".format(case, val))
         cout.send(workerfn(val))
Esempio n. 8
0
 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...')
Esempio n. 9
0
 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...')
Esempio n. 10
0
    def test_select(self):
        # https://gobyexample.com/select
        c1 = goless.chan()
        c2 = goless.chan()

        def func1():
            time.sleep(.1)
            c1.send('one')
        goless.go(func1)

        def func2():
            time.sleep(.2)
            c2.send('two')
        goless.go(func2)

        # We don't print since we run this as a test.
        callbacks = []

        for i in range(2):
            _, val = goless.select([goless.rcase(c1), goless.rcase(c2)])
            callbacks.append(val)

        self.assertEqual(callbacks, ['one', 'two'])
Esempio n. 11
0
    def test_select(self):
        # https://gobyexample.com/select
        c1 = goless.chan()
        c2 = goless.chan()

        def func1():
            time.sleep(.1)
            c1.send('one')
        goless.go(func1)

        def func2():
            time.sleep(.2)
            c2.send('two')
        goless.go(func2)

        # We don't print since we run this as a test.
        callbacks = []

        for i in range(2):
            _, val = goless.select([goless.rcase(c1), goless.rcase(c2)])
            callbacks.append(val)

        self.assertEqual(callbacks, ['one', 'two'])
Esempio n. 12
0
      print("{}[{}] got case={}, val={}".format(name, params['val'], case, val))
      time.sleep(params['sleep'])
      print("{}[{}] sending to cout...".format(name, params['val']))
      params['cout'].send("(" + name + ")=" + val + ";" + params['val'])
  return func1

funcs = [makeWorker({
  'name': 'workerA',
  'cin':  channels[0],
  'cout': channels[1],
  'val':  'one',
  'sleep': 1
  }), 
  makeWorker({
  'name': 'workerB',
  'cin':  channels[1],
  'cout': channels[2],
  'val':  'two',
  'sleep': 1
    })]

for f in funcs:
  goless.go(f)

channels[0].send('(main)=inval')

for i in range(1):
    case, val = goless.select([goless.rcase(channels[2])])
    print("result={}".format(val))

Esempio n. 13
0
 def setUp(self):
     BaseTests.setUp(self)
     self.ch = goless.chan(self.chansize)
     self.ca = goless.rcase(self.ch)
Esempio n. 14
0
 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)
Esempio n. 15
0
 def test_exec_with_no_body(self):
     be.run(self.ch.send, 'a')
     ca = goless.rcase(self.ch)
     self.assertEqual(ca.exec_(), 'a')
Esempio n. 16
0
 def test_raises_deadlock_if_no_goroutines(self):
     with self.assertRaises(goless.Deadlock):
         goless.select(goless.rcase(goless.chan()))
Esempio n. 17
0
 def test_exec_with_no_body(self):
     be.run(self.ch.send, 'a')
     ca = goless.rcase(self.ch)
     self.assertEqual(ca.exec_(), 'a')
Esempio n. 18
0
 def setUp(self):
     BaseTests.setUp(self)
     self.ch = goless.chan(self.chansize)
     self.ca = goless.rcase(self.ch)
Esempio n. 19
0
            if sensor_value:
                channel.send((sensor_mode[1], sensor_value))
                if isinstance(sensor_value, brickpi3.SensorError):
                    break


def read_sensor(brick, port, sensor_type):
    try:
        brick.set_sensor_type(port, sensor_type)
        time.sleep(0.01)
        return brick.get_sensor(port)
    except brickpi3.SensorError as error:
        print("error infrared", error)
        return error


if __name__ == '__main__':
    print('for local testing read 100 infrared readings from port 1')
    brick = brickpi3.BrickPi3()
    readings = goless.chan()

    print('start infrared sensor')
    start_infrared_sensor(brick, brick.PORT_2, readings)

    for i in range(100):
        case, val = goless.select([goless.rcase(readings)])
        print(case, val)

    print('100 reading are done, time to clean and exit')
    brick.reset_all()
Esempio n. 20
0
 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)
Esempio n. 21
0
 def test_raises_deadlock_if_no_goroutines(self):
     with self.assertRaises(goless.Deadlock):
         goless.select(goless.rcase(goless.chan()))