|
| 1 | +require "./spec_helper" |
| 2 | + |
| 3 | +def expect_channel_eq(channel, value) |
| 4 | + select |
| 5 | + when x = channel.receive |
| 6 | + x.should eq(value) |
| 7 | + else |
| 8 | + raise "A value was expected" |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +def expect_channel_be(channel, value) |
| 13 | + select |
| 14 | + when x = channel.receive |
| 15 | + x.should be(value) |
| 16 | + else |
| 17 | + raise "A value was expected" |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +describe Marmot do |
| 22 | + describe "#repeat" do |
| 23 | + it "schedules a new task that repeats" do |
| 24 | + channel = Channel(Int32).new |
| 25 | + |
| 26 | + x = 0 |
| 27 | + task = Marmot.repeat(10.milliseconds) do |
| 28 | + x += 1 |
| 29 | + channel.send(x) |
| 30 | + end |
| 31 | + spawn Marmot.run |
| 32 | + |
| 33 | + sleep 15.milliseconds |
| 34 | + expect_channel_eq(channel, 1) |
| 35 | + sleep 10.milliseconds |
| 36 | + expect_channel_eq(channel, 2) |
| 37 | + sleep 10.milliseconds |
| 38 | + expect_channel_eq(channel, 3) |
| 39 | + |
| 40 | + task.cancel |
| 41 | + channel.close |
| 42 | + Marmot.stop |
| 43 | + end |
| 44 | + |
| 45 | + it "runs on first run if specified" do |
| 46 | + channel = Channel(Int32).new |
| 47 | + |
| 48 | + x = 0 |
| 49 | + task = Marmot.repeat(10.milliseconds, true) do |
| 50 | + x += 1 |
| 51 | + channel.send(x) |
| 52 | + end |
| 53 | + spawn Marmot.run |
| 54 | + |
| 55 | + sleep 5.milliseconds |
| 56 | + expect_channel_eq(channel, 1) |
| 57 | + |
| 58 | + task.cancel |
| 59 | + channel.close |
| 60 | + Marmot.stop |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + describe "#cron" do |
| 65 | + it "schedules a new task" do |
| 66 | + channel = Channel(Int32).new |
| 67 | + |
| 68 | + time = Time.local.at_beginning_of_second + 2.second |
| 69 | + task = Marmot.cron(time.hour, time.minute, time.second) { channel.send(1) } |
| 70 | + spawn Marmot.run |
| 71 | + |
| 72 | + sleep (time - Time.local + 5.milliseconds) |
| 73 | + expect_channel_eq(channel, 1) |
| 74 | + |
| 75 | + task.cancel |
| 76 | + channel.close |
| 77 | + Marmot.stop |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + describe "#run" do |
| 82 | + it "runs a task without arguments" do |
| 83 | + channel = Channel(Int32).new |
| 84 | + |
| 85 | + task = Marmot.repeat(3.milliseconds) { channel.send(1) } |
| 86 | + spawn Marmot.run |
| 87 | + |
| 88 | + sleep 5.milliseconds |
| 89 | + expect_channel_eq(channel, 1) |
| 90 | + |
| 91 | + task.cancel |
| 92 | + channel.close |
| 93 | + Marmot.stop |
| 94 | + end |
| 95 | + |
| 96 | + it "runs a task with one argument and gives it its Task object" do |
| 97 | + channel = Channel(Marmot::Task).new |
| 98 | + |
| 99 | + task = Marmot.repeat(3.milliseconds) { |t| channel.send(t) } |
| 100 | + spawn Marmot.run |
| 101 | + |
| 102 | + sleep 5.milliseconds |
| 103 | + expect_channel_be(channel, task) |
| 104 | + |
| 105 | + task.cancel |
| 106 | + channel.close |
| 107 | + Marmot.stop |
| 108 | + end |
| 109 | + |
| 110 | + it "stops canceled tasks" do |
| 111 | + channel = Channel(Marmot::Task).new |
| 112 | + |
| 113 | + task = Marmot.repeat(3.milliseconds) do |t| |
| 114 | + t.cancel |
| 115 | + channel.close |
| 116 | + end |
| 117 | + spawn Marmot.run |
| 118 | + |
| 119 | + sleep 5.milliseconds |
| 120 | + channel.closed?.should be_true |
| 121 | + task.canceled?.should be_true |
| 122 | + |
| 123 | + Marmot.stop |
| 124 | + end |
| 125 | + |
| 126 | + it "does not run when there is no tasks" do |
| 127 | + channel = Channel(Int32).new |
| 128 | + |
| 129 | + task = Marmot.repeat(1.milliseconds) { } |
| 130 | + task.cancel |
| 131 | + |
| 132 | + spawn do |
| 133 | + Marmot.run |
| 134 | + channel.send(1) |
| 135 | + end |
| 136 | + |
| 137 | + Fiber.yield |
| 138 | + expect_channel_eq(channel, 1) |
| 139 | + channel.close |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + describe "#stop" do |
| 144 | + it "stops the tasks but does not cancel them" do |
| 145 | + channel = Channel(Int32).new |
| 146 | + |
| 147 | + task = Marmot.repeat(10.milliseconds) { channel.send(1) } |
| 148 | + spawn do |
| 149 | + Marmot.run |
| 150 | + channel.send(2) |
| 151 | + end |
| 152 | + |
| 153 | + sleep 15.milliseconds |
| 154 | + expect_channel_eq(channel, 1) |
| 155 | + |
| 156 | + Marmot.stop |
| 157 | + Fiber.yield |
| 158 | + expect_channel_eq(channel, 2) |
| 159 | + |
| 160 | + task.canceled?.should be_false |
| 161 | + end |
| 162 | + end |
| 163 | +end |
0 commit comments