예제 #1
0
 def test_select_raises_for_list_and_args(self):
     chan1 = goless.chan(1)
     chan2 = goless.chan(1)
     chan3 = goless.chan(1)
     cases = [goless.scase(chan1, 1), goless.scase(chan2, 2)]
     
     with self.assertRaises(TypeError):
         goless.select(cases, chan3)
예제 #2
0
    def test_select_raises_for_list_and_args(self):
        chan1 = goless.chan(1)
        chan2 = goless.chan(1)
        chan3 = goless.chan(1)
        cases = [goless.scase(chan1, 1), goless.scase(chan2, 2)]

        with self.assertRaises(TypeError):
            goless.select(cases, chan3)
예제 #3
0
파일: ex1.py 프로젝트: kyleburton/sandbox
 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'])
예제 #4
0
파일: main.py 프로젝트: kyleburton/sandbox
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")
예제 #5
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)
예제 #6
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)
예제 #7
0
파일: main.py 프로젝트: kyleburton/sandbox
 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))
예제 #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...')
예제 #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...')
예제 #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'])
예제 #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'])
예제 #12
0
 def test_main_tasklet_can_select(self):
     chan1 = goless.chan(1)
     cases = [goless.scase(chan1, 3)]
     chosen, val = goless.select(cases)
     self.assertIs(chosen, cases[0])
     self.assertIsNone(val)
예제 #13
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)
예제 #14
0
파일: ex1.py 프로젝트: kyleburton/sandbox
      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))

예제 #15
0
 def test_select_with_no_args_should_do_nothing(self):
     goless.select()
     goless.select([])
예제 #16
0
 def test_raises_deadlock_if_no_goroutines(self):
     with self.assertRaises(goless.Deadlock):
         goless.select(goless.rcase(goless.chan()))
예제 #17
0
 def test_select_accepts_args(self):
     chan1 = goless.chan(1)
     scase = goless.scase(chan1, 1)
     chosen, val = goless.select(scase)
     self.assertIs(chosen, scase)
     self.assertIsNone(val)
예제 #18
0
 def test_select_accepts_args(self):
     chan1 = goless.chan(1)
     scase = goless.scase(chan1, 1)
     chosen, val = goless.select(scase)
     self.assertIs(chosen, scase)
     self.assertIsNone(val)
예제 #19
0
 def test_main_tasklet_can_select(self):
     chan1 = goless.chan(1)
     cases = [goless.scase(chan1, 3)]
     chosen, val = goless.select(cases)
     self.assertIs(chosen, cases[0])
     self.assertIsNone(val)
예제 #20
0
 def test_raises_if_multiple_default_cases(self):
     with self.assertRaises(AssertionError):
         goless.select([goless.dcase(), goless.dcase()])
예제 #21
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()
예제 #22
0
 def sel():
     a.append(goless.select(cases))
예제 #23
0
 def test_select_with_no_args_should_do_nothing(self):
     goless.select()
     goless.select([])
예제 #24
0
 def test_raises_if_multiple_default_cases(self):
     with self.assertRaises(AssertionError):
         goless.select([goless.dcase(), goless.dcase()])
예제 #25
0
 def test_raises_deadlock_if_no_goroutines(self):
     with self.assertRaises(goless.Deadlock):
         goless.select(goless.rcase(goless.chan()))
예제 #26
0
 def sel():
     a.append(goless.select(cases))
예제 #27
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)