• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-concurrency / concurrent-ruby / #2835

05 Oct 2014 10:16PM UTC coverage: 45.201% (-49.6%) from 94.81%
#2835

push

jdantonio
Merge pull request #158 from obrok/promise-composition

Promise composition

2 of 15 new or added lines in 1 file covered. (13.33%)

1514 existing lines in 84 files now uncovered.

1375 of 3042 relevant lines covered (45.2%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.89
/lib/concurrent/collection/blocking_ring_buffer.rb
1
require 'concurrent/atomic/condition'
1✔
2

3
module Concurrent
1✔
4
  class BlockingRingBuffer
1✔
5

6
    def initialize(capacity)
1✔
UNCOV
7
      @buffer = RingBuffer.new(capacity)
×
UNCOV
8
      @first = @last = 0
×
UNCOV
9
      @count = 0
×
UNCOV
10
      @mutex = Mutex.new
×
UNCOV
11
      @condition = Condition.new
×
12
    end
13

14
    # @return [Integer] the capacity of the buffer
15
    def capacity
1✔
UNCOV
16
      @mutex.synchronize { @buffer.capacity }
×
17
    end
18

19
    # @return [Integer] the number of elements currently in the buffer
20
    def count
1✔
UNCOV
21
      @mutex.synchronize { @buffer.count }
×
22
    end
23

24
    # @return [Boolean] true if buffer is empty, false otherwise
25
    def empty?
1✔
UNCOV
26
      @mutex.synchronize { @buffer.empty? }
×
27
    end
28

29
    # @return [Boolean] true if buffer is full, false otherwise
30
    def full?
1✔
UNCOV
31
      @mutex.synchronize { @buffer.full? }
×
32
    end
33

34
    # @param [Object] value the value to be inserted
35
    # @return [Boolean] true if value has been inserted, false otherwise
36
    def put(value)
1✔
UNCOV
37
      @mutex.synchronize do
×
UNCOV
38
        wait_while_full
×
UNCOV
39
        @buffer.offer(value)
×
UNCOV
40
        @condition.signal
×
UNCOV
41
        true
×
42
      end
43
    end
44

45
    # @return [Object] the first available value and removes it from the buffer. If buffer is empty it blocks until an element is available
46
    def take
1✔
UNCOV
47
      @mutex.synchronize do
×
UNCOV
48
        wait_while_empty
×
UNCOV
49
        result = @buffer.poll
×
UNCOV
50
        @condition.signal
×
UNCOV
51
        result
×
52
      end
53
    end
54

55
    # @return [Object] the first available value and without removing it from the buffer. If buffer is empty returns nil
56
    def peek
1✔
UNCOV
57
      @mutex.synchronize { @buffer.peek }
×
58
    end
59

60
    private
1✔
61

62
    def wait_while_full
1✔
UNCOV
63
      @condition.wait(@mutex) while @buffer.full?
×
64
    end
65

66
    def wait_while_empty
1✔
UNCOV
67
      @condition.wait(@mutex) while @buffer.empty?
×
68
    end
69

70
  end
71
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc