OLD | NEW |
(Empty) | |
| 1 # Copyright 2015, Google Inc. |
| 2 # All rights reserved. |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 require 'grpc' |
| 31 |
| 32 describe GRPC::Pool do |
| 33 Pool = GRPC::Pool |
| 34 |
| 35 describe '#new' do |
| 36 it 'raises if a non-positive size is used' do |
| 37 expect { Pool.new(0) }.to raise_error |
| 38 expect { Pool.new(-1) }.to raise_error |
| 39 expect { Pool.new(Object.new) }.to raise_error |
| 40 end |
| 41 |
| 42 it 'is constructed OK with a positive size' do |
| 43 expect { Pool.new(1) }.not_to raise_error |
| 44 end |
| 45 end |
| 46 |
| 47 describe '#jobs_waiting' do |
| 48 it 'at start, it is zero' do |
| 49 p = Pool.new(1) |
| 50 expect(p.jobs_waiting).to be(0) |
| 51 end |
| 52 |
| 53 it 'it increases, with each scheduled job if the pool is not running' do |
| 54 p = Pool.new(1) |
| 55 job = proc {} |
| 56 expect(p.jobs_waiting).to be(0) |
| 57 5.times do |i| |
| 58 p.schedule(&job) |
| 59 expect(p.jobs_waiting).to be(i + 1) |
| 60 end |
| 61 end |
| 62 |
| 63 it 'it decreases as jobs are run' do |
| 64 p = Pool.new(1) |
| 65 job = proc {} |
| 66 expect(p.jobs_waiting).to be(0) |
| 67 3.times do |
| 68 p.schedule(&job) |
| 69 end |
| 70 p.start |
| 71 sleep 2 |
| 72 expect(p.jobs_waiting).to be(0) |
| 73 end |
| 74 end |
| 75 |
| 76 describe '#schedule' do |
| 77 it 'return if the pool is already stopped' do |
| 78 p = Pool.new(1) |
| 79 p.stop |
| 80 job = proc {} |
| 81 expect { p.schedule(&job) }.to_not raise_error |
| 82 end |
| 83 |
| 84 it 'adds jobs that get run by the pool' do |
| 85 p = Pool.new(1) |
| 86 p.start |
| 87 o, q = Object.new, Queue.new |
| 88 job = proc { q.push(o) } |
| 89 p.schedule(&job) |
| 90 expect(q.pop).to be(o) |
| 91 p.stop |
| 92 end |
| 93 end |
| 94 |
| 95 describe '#stop' do |
| 96 it 'works when there are no scheduled tasks' do |
| 97 p = Pool.new(1) |
| 98 expect { p.stop }.not_to raise_error |
| 99 end |
| 100 |
| 101 it 'stops jobs when there are long running jobs' do |
| 102 p = Pool.new(1) |
| 103 p.start |
| 104 o, q = Object.new, Queue.new |
| 105 job = proc do |
| 106 sleep(5) # long running |
| 107 q.push(o) |
| 108 end |
| 109 p.schedule(&job) |
| 110 sleep(1) # should ensure the long job gets scheduled |
| 111 expect { p.stop }.not_to raise_error |
| 112 end |
| 113 end |
| 114 |
| 115 describe '#start' do |
| 116 it 'runs pre-scheduled jobs' do |
| 117 p = Pool.new(2) |
| 118 o, q = Object.new, Queue.new |
| 119 n = 5 # arbitrary |
| 120 n.times { p.schedule(o, &q.method(:push)) } |
| 121 p.start |
| 122 n.times { expect(q.pop).to be(o) } |
| 123 p.stop |
| 124 end |
| 125 |
| 126 it 'runs jobs as they are scheduled ' do |
| 127 p = Pool.new(2) |
| 128 o, q = Object.new, Queue.new |
| 129 p.start |
| 130 n = 5 # arbitrary |
| 131 n.times do |
| 132 p.schedule(o, &q.method(:push)) |
| 133 expect(q.pop).to be(o) |
| 134 end |
| 135 p.stop |
| 136 end |
| 137 end |
| 138 end |
OLD | NEW |