File: timing_buffer_shared.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; ansic: 288; makefile: 9; sh: 6
file content (167 lines) | stat: -rw-r--r-- 3,747 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require_relative 'base_shared'

RSpec.shared_examples :channel_timing_buffer do

  specify do
    expect(subject).to be_blocking
  end

  context '#capacity' do
    specify do
      expect(subject.capacity).to eq 1
    end
  end

  context '#size' do
    specify do
      expect(subject.size).to eq 0
    end
  end

  context '#empty?' do
    specify do
      expect(subject).to_not be_empty
    end
  end

  context '#full?' do
    specify do
      expect(subject).to be_full
    end
  end

  context '#put' do
    specify do
      expect(subject.put(:foo)).to be false
    end
  end

  context '#offer' do
    specify do
      expect(subject.offer(:foo)).to be false
    end
  end

  context '#take' do

    it 'blocks when the timer is not ready' do
      actual = Concurrent::AtomicBoolean.new(false)
      subject = described_class.new(10)
      t = in_thread do
        subject.take
        actual.make_true
      end
      t.join(0.1)
      actual = actual.value
      t.kill # clean up
      expect(actual).to be false
    end

    it 'returns a Tick' do
      subject = described_class.new(0.1)
      expect(subject.take).to be_a Concurrent::Channel::Tick
    end

    it 'triggers after the specified time interval' do
      start = Concurrent::Channel::Tick.new.monotonic
      subject = described_class.new(0.1)
      actual = subject.take.monotonic
      expect(actual - start).to be >= 0.1
    end

    it 'returns Concurrent::NULL when closed' do
      subject.close
      expect(subject.take).to eq Concurrent::NULL
    end
  end

  context '#poll' do

    it 'returns Concurrent::NULL when the timer is not ready' do
      subject = described_class.new(0.1)
      expect(subject.poll).to eq Concurrent::NULL
    end

    it 'returns a Tick' do
      subject = described_class.new(0.1)
      sleep(0.2)
      expect(subject.poll).to be_a Concurrent::Channel::Tick
    end

    it 'returns Concurrent::NULL when closed' do
      subject.close
      expect(subject.poll).to eq Concurrent::NULL
    end

    it 'triggers after the specified time interval' do
      start = Concurrent::Channel::Tick.new.monotonic
      subject = described_class.new(0.1)
      sleep(0.2)
      actual = subject.poll.monotonic
      expect(actual - start).to be >= 0.1
    end
  end

  context '#next' do

    it 'blocks when the timer is not ready' do
      actual = Concurrent::AtomicBoolean.new(false)
      subject = described_class.new(10)
      t = in_thread do
        subject.next
        actual.make_true
      end
      t.join(0.1)
      actual = actual.value
      t.kill # clean up
      expect(actual).to be false
    end

    it 'returns a Tick when open' do
      subject = described_class.new(0.1)
      value, _ = subject.next
      expect(value).to be_a Concurrent::Channel::Tick
    end

    it 'returns Concurrent::NULL, false when closed' do
      subject.close
      expect(subject.take).to eq Concurrent::NULL
    end

    it 'triggers after the specified time interval' do
      start = Concurrent::Channel::Tick.new.monotonic
      subject = described_class.new(0.1)
      actual, _ = subject.next
      expect(actual.monotonic - start).to be >= 0.1
    end
  end

  context '#close' do

    it 'sets #closed? to false' do
      subject.close
      expect(subject).to be_closed
    end

    it 'returns true when not previously closed' do
      expect(subject.close).to be true
    end

    it 'returns false when already closed' do
      subject.close
      expect(subject.close).to be false
    end
  end

  context '#closed?' do

    it 'returns true when new' do
      expect(subject).to_not be_closed
    end

    it 'returns false after #close' do
      subject.close
      expect(subject).to be_closed
    end
  end
end