File: session_spec.rb

package info (click to toggle)
ruby-mongo 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: ruby: 45,579; makefile: 5
file content (252 lines) | stat: -rw-r--r-- 6,102 bytes parent folder | download | duplicates (3)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require 'spec_helper'

describe Mongo::Session, if: test_sessions? do

  let(:session) do
    authorized_client.start_session(options)
  end

  let(:options) do
    {}
  end

  describe '#initialize' do

    context 'when options are provided' do

      it 'duplicates and freezes the options' do
        expect(session.options).not_to be(options)
        expect(session.options.frozen?).to be(true)
      end
    end

    it 'sets a server session with an id' do
      expect(session.session_id).to be_a(BSON::Document)
    end

    it 'sets the cluster time to nil' do
      expect(session.cluster_time).to be(nil)
    end

    it 'sets the cluster' do
      expect(session.cluster).to be(authorized_client.cluster)
    end
  end

  describe '#inspect' do

    it 'includes the Ruby object_id in the formatted string' do
      expect(session.inspect).to include(session.object_id.to_s)
    end

    it 'includes the session_id in the formatted string' do
      expect(session.inspect).to include(session.session_id.to_s)
    end

    context 'when options are provided' do

      let(:options) do
        { causal_consistency: true }
      end

      it 'includes the options in the formatted string' do
        expect(session.inspect).to include({ implicit: false,
                                             causal_consistency: true }.to_s)
      end
    end
  end

  describe '#advance_cluster_time' do

    let(:new_cluster_time) do
      { 'clusterTime' => BSON::Timestamp.new(0, 5) }
    end

    context 'when the session does not have a cluster time' do

      before do
        session.advance_cluster_time(new_cluster_time)
      end

      it 'sets the new cluster time' do
        expect(session.cluster_time).to eq(new_cluster_time)
      end
    end

    context 'when the session already has a cluster time' do

      context 'when the original cluster time is less than the new cluster time' do

        let(:original_cluster_time) do
          { 'clusterTime' => BSON::Timestamp.new(0, 1) }
        end

        before do
          session.instance_variable_set(:@cluster_time, original_cluster_time)
          session.advance_cluster_time(new_cluster_time)
        end

        it 'sets the new cluster time' do
          expect(session.cluster_time).to eq(new_cluster_time)
        end
      end

      context 'when the original cluster time is equal or greater than the new cluster time' do

        let(:original_cluster_time) do
          { 'clusterTime' => BSON::Timestamp.new(0, 6) }
        end

        before do
          session.instance_variable_set(:@cluster_time, original_cluster_time)
          session.advance_cluster_time(new_cluster_time)
        end

        it 'does not update the cluster time' do
          expect(session.cluster_time).to eq(original_cluster_time)
        end
      end
    end
  end

  describe '#advance_operation_time' do

    let(:new_operation_time) do
      BSON::Timestamp.new(0, 5)
    end

    context 'when the session does not have an operation time' do

      before do
        session.advance_operation_time(new_operation_time)
      end

      it 'sets the new operation time' do
        expect(session.operation_time).to eq(new_operation_time)
      end
    end

    context 'when the session already has an operation time' do

      context 'when the original operation time is less than the new operation time' do

        let(:original_operation_time) do
          BSON::Timestamp.new(0, 1)
        end

        before do
          session.instance_variable_set(:@operation_time, original_operation_time)
          session.advance_operation_time(new_operation_time)
        end

        it 'sets the new operation time' do
          expect(session.operation_time).to eq(new_operation_time)
        end
      end

      context 'when the original operation time is equal or greater than the new operation time' do

        let(:original_operation_time) do
          BSON::Timestamp.new(0, 6)
        end

        before do
          session.instance_variable_set(:@operation_time, original_operation_time)
          session.advance_operation_time(new_operation_time)
        end

        it 'does not update the operation time' do
          expect(session.operation_time).to eq(original_operation_time)
        end
      end
    end
  end

  describe 'ended?' do

    context 'when the session has not been ended' do

      it 'returns false' do
        expect(session.ended?).to be(false)
      end
    end

    context 'when the session has been ended' do

      before do
        session.end_session
      end

      it 'returns true' do
        expect(session.ended?).to be(true)
      end
    end
  end

  describe 'end_session' do

    let!(:server_session) do
      session.instance_variable_get(:@server_session)
    end

    let(:cluster_session_pool) do
      session.cluster.session_pool
    end

    it 'returns the server session to the cluster session pool' do
      session.end_session
      expect(cluster_session_pool.instance_variable_get(:@queue)).to include(server_session)
    end

    context 'when #end_session is called multiple times' do

      before do
        session.end_session
      end

      it 'returns nil' do
        expect(session.end_session).to be_nil
      end
    end
  end

  describe '#retry_writes?', if: test_sessions? do

    context 'when the option is set to true' do

      let(:client) do
        authorized_client_with_retry_writes
      end

      it 'returns true' do
        expect(client.start_session.retry_writes?).to be(true)
      end
    end

    context 'when the option is set to false' do

      let(:client) do
        authorized_client.with(retry_writes: false)
      end

      after do
        client.close
      end

      it 'returns false' do
        expect(client.start_session.retry_writes?).to be(false)
      end
    end

    context 'when the option is not defined' do

      let(:client) do
        authorized_client
      end

      it 'returns false' do
        expect(client.start_session.retry_writes?).to be(false)
      end
    end
  end
end