File: context_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (79 lines) | stat: -rw-r--r-- 1,931 bytes parent folder | download
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
# frozen_string_literal: true

require 'lite_spec_helper'

describe Mongo::Operation::Context do
  describe '#initialize' do
    context 'when timeout_ms is negative' do
      it 'raises an error' do
        expect do
          described_class.new(operation_timeouts: { operation_timeout_ms: -1 })
        end.to raise_error ArgumentError, /must be a non-negative integer/
      end
    end
  end

  describe '#deadline' do
    let(:context) { described_class.new(operation_timeouts: { operation_timeout_ms: timeout_ms }) }

    context 'when timeout_ms is nil' do
      let(:timeout_ms) { nil }

      it 'returns nil' do
        expect(context.deadline).to be_nil
      end
    end

    context 'when timeout_ms is zero' do
      let(:timeout_ms) { 0 }

      it 'returns nil' do
        expect(context.deadline).to eq(0)
      end
    end

    context 'when timeout_ms is positive' do
      before do
        allow(Mongo::Utils).to receive(:monotonic_time).and_return(100.0)
      end

      let(:timeout_ms) { 10_000 }

      it 'calculates the deadline' do
        expect(context.deadline).to eq(110)
      end
    end
  end

  describe '#remaining_timeout_ms' do
    let(:context) { described_class.new(operation_timeouts: { operation_timeout_ms: timeout_ms }) }

    context 'when timeout_ms is nil' do
      let(:timeout_ms) { nil }

      it 'returns nil' do
        expect(context.remaining_timeout_ms).to be_nil
      end
    end

    context 'when timeout_ms is zero' do
      let(:timeout_ms) { 0 }

      it 'returns nil' do
        expect(context.remaining_timeout_ms).to be_nil
      end
    end

    context 'when timeout_ms is positive' do
      before do
        allow(Mongo::Utils).to receive(:monotonic_time).and_return(100.0, 105.0)
      end

      let(:timeout_ms) { 10_000 }

      it 'calculates the remaining time' do
        expect(context.remaining_timeout_ms).to eq(5_000)
      end
    end
  end
end