File: compressed_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 (83 lines) | stat: -rw-r--r-- 2,110 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
80
81
82
83
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe Mongo::Protocol::Compressed do

  let(:original_message) { Mongo::Protocol::Query.new(SpecConfig.instance.test_db, 'protocol-test', { ping: 1 }) }
  let(:compressor) { 'zlib' }
  let(:level)      { nil }

  let(:message) do
    described_class.new(original_message, compressor, level)
  end

  let(:original_message_bytes) do
    buf = BSON::ByteBuffer.new
    original_message.send(:serialize_fields, buf)
    buf.to_s
  end

  describe '#serialize' do

    context "when using the snappy compressor" do
      require_snappy_compression
      let(:compressor) { 'snappy' }

      it "uses snappy" do
        expect(Snappy).to receive(:deflate).with(original_message_bytes).and_call_original
        message.serialize
      end
    end

    context "when using the zstd compressor" do
      require_zstd_compression
      let(:compressor) { 'zstd' }

      it "uses zstd with default compression level" do
        expect(Zstd).to receive(:compress).with(original_message_bytes).and_call_original
        message.serialize
      end
    end

    context 'when zlib compression level is not provided' do

      it 'does not set a compression level' do
        expect(Zlib::Deflate).to receive(:deflate).with(original_message_bytes, nil).and_call_original
        message.serialize
      end
    end

    context 'when zlib compression level is provided' do

      let(:level) { 1 }

      it 'uses the compression level' do
        expect(Zlib::Deflate).to receive(:deflate).with(original_message_bytes, 1).and_call_original
        message.serialize
      end
    end
  end

  describe '#replyable?' do

    context 'when the original message is replyable' do

      it 'returns true' do
        expect(message.replyable?).to be(true)
      end
    end

    context 'when the original message is not replyable' do

      let(:original_message) do
        Mongo::Protocol::Msg.new([:more_to_come], {}, { ping: 1 })
      end

      it 'returns false' do
        expect(message.replyable?).to be(false)
      end
    end
  end
end