summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/subscribers/action_cable_spec.rb
blob: 08437920e0ca93597d7c08eb137a8a07a23b9aaa (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::Subscribers::ActionCable, :request_store do
  let(:subscriber) { described_class.new }
  let(:counter) { double(:counter) }
  let(:data) { { 'result' => { 'data' => { 'event' => 'updated' } } } }
  let(:channel_class) { 'IssuesChannel' }
  let(:event) do
    double(
      :event,
      name: name,
      payload: payload
    )
  end

  describe '#transmit' do
    let(:name) { 'transmit.action_cable' }
    let(:via) { 'streamed from issues:Z2lkOi8vZs2l0bGFiL0lzc3VlLzQ0Ng' }
    let(:payload) do
      {
        channel_class: channel_class,
        via: via,
        data: data
      }
    end

    it 'tracks the transmit event' do
      allow(::Gitlab::Metrics).to receive(:counter).with(
        :action_cable_single_client_transmissions_total, /transmit/
      ).and_return(counter)

      expect(counter).to receive(:increment)

      subscriber.transmit(event)
    end

    it 'tracks size of payload as JSON' do
      allow(::Gitlab::Metrics).to receive(:histogram).with(
        :action_cable_transmitted_bytes, /transmit/
      ).and_return(counter)
      message_size = ::Gitlab::Json.generate(data).bytesize

      expect(counter).to receive(:observe).with({ channel: channel_class, operation: 'event' }, message_size)

      subscriber.transmit(event)
    end
  end

  describe '#broadcast' do
    let(:name) { 'broadcast.action_cable' }
    let(:coder) { ActiveSupport::JSON }
    let(:message) do
      { event: :updated }
    end

    let(:broadcasting) { 'issues:Z2lkOi8vZ2l0bGFiL0lzc3VlLzQ0Ng' }
    let(:payload) do
      {
        broadcasting: broadcasting,
        message: message,
        coder: coder
      }
    end

    it 'tracks the broadcast event' do
      allow(::Gitlab::Metrics).to receive(:counter).with(
        :action_cable_broadcasts_total, /broadcast/
      ).and_return(counter)

      expect(counter).to receive(:increment)

      subscriber.broadcast(event)
    end
  end

  describe '#transmit_subscription_confirmation' do
    let(:name) { 'transmit_subscription_confirmation.action_cable' }
    let(:channel_class) { 'IssuesChannel' }
    let(:payload) do
      {
        channel_class: channel_class
      }
    end

    it 'tracks the subscription confirmation event' do
      allow(::Gitlab::Metrics).to receive(:counter).with(
        :action_cable_subscription_confirmations_total, /confirm/
      ).and_return(counter)

      expect(counter).to receive(:increment)

      subscriber.transmit_subscription_confirmation(event)
    end
  end

  describe '#transmit_subscription_rejection' do
    let(:name) { 'transmit_subscription_rejection.action_cable' }
    let(:channel_class) { 'IssuesChannel' }
    let(:payload) do
      {
          channel_class: channel_class
      }
    end

    it 'tracks the subscription rejection event' do
      allow(::Gitlab::Metrics).to receive(:counter).with(
        :action_cable_subscription_rejections_total, /reject/
      ).and_return(counter)

      expect(counter).to receive(:increment)

      subscriber.transmit_subscription_rejection(event)
    end
  end
end