|
| 1 | +from base import BaseCspTest, main |
| 2 | +from csp.csp import Channel, Par |
| 3 | + |
| 4 | +class TestChannels(BaseCspTest): |
| 5 | + |
| 6 | + def test_single_source_sink_channel(self): |
| 7 | + chan = Channel() |
| 8 | + self.source('012345', chan) // (self.sink(6, chan), ) |
| 9 | + self.assertEquals(self.output(), '012345') |
| 10 | + |
| 11 | + def test_multiple_source_sink_channel_triples(self): |
| 12 | + chans = [Channel() for x in range(5)] |
| 13 | + srces = Par(*[self.source(str(ii), ch) for ii, ch in enumerate(chans)]) |
| 14 | + sinks = Par(*[self.sink(1, ch) for ch in chans]) |
| 15 | + Par(srces, sinks).start() |
| 16 | + self.assertEquals(''.join(sorted(self.output())), '01234') |
| 17 | + |
| 18 | + def test_multiple_source_sink_tuples_one_channel(self): |
| 19 | + channel = Channel() |
| 20 | + srces = Par(*[self.source(str(ii), channel) for ii in range(5)]) |
| 21 | + sinks = Par(*[self.sink(1, channel) for ii in range(5)]) |
| 22 | + Par(srces, sinks).start() |
| 23 | + self.assertEquals(''.join(sorted(self.output())), '01234') |
| 24 | + |
| 25 | + |
| 26 | +class TestPoison(BaseCspTest): |
| 27 | + |
| 28 | + def test_poison_aborts_channel(self): |
| 29 | + channel = Channel() |
| 30 | + channel.poison() |
| 31 | + Par(self.source('012345', channel), |
| 32 | + self.sink(6, channel)).start() |
| 33 | + self.assertEquals(self.output(), '') |
| 34 | + |
| 35 | + def test_poison_does_not_abort_other_channels(self): |
| 36 | + poisoned_channel = Channel() |
| 37 | + clean_channel = Channel() |
| 38 | + poisoned_channel.poison() |
| 39 | + Par(self.source('012345', clean_channel), |
| 40 | + self.sink(6, clean_channel)).start() |
| 41 | + self.assertEquals(self.output(), '012345') |
| 42 | + |
| 43 | + |
| 44 | +class TestOverread(BaseCspTest): |
| 45 | + |
| 46 | + def do_not_test_overread(self): # as it will hang |
| 47 | + channel = Channel() |
| 48 | + self.source('012345', channel) // (self.sink(12, channel), ) |
| 49 | + self.assertEquals(self.output(), '012345') |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + main() |
0 commit comments