Skip to content

Commit

Permalink
Edits and improvements to webhook events (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk authored Aug 3, 2024
1 parent 4b5a80b commit f19a9b8
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 20 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ group :development do
gem 'rubocop-rake', '~> 0.6.0'
gem 'rubocop-rspec', '~> 3.0'
gem 'super_diff', '~> 0.12.1'
gem 'webmock', '~> 3.23', '>= 3.23.1'
end

# NOTE: An extremely platform-dependent gem that is needed only for development.
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ GEM
concurrent-ruby (1.3.3)
config (5.5.1)
deep_merge (~> 1.2, >= 1.2.1)
crack (1.0.0)
bigdecimal
rexml
deep_merge (1.2.2)
diff-lcs (1.5.1)
dotenv (3.1.2)
Expand All @@ -23,6 +26,7 @@ GEM
net-http
ffi (1.17.0)
ffi (1.17.0-x86_64-linux-gnu)
hashdiff (1.1.1)
ipaddr (1.2.6)
json (2.7.2)
json-schema (4.3.1)
Expand Down Expand Up @@ -136,6 +140,10 @@ GEM
tilt (2.3.0)
unicode-display_width (2.5.0)
uri (0.13.0)
webmock (3.23.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby
Expand Down Expand Up @@ -166,6 +174,7 @@ DEPENDENCIES
sinatra-contrib (~> 4.0)
stackprof (~> 0.2.26)
super_diff (~> 0.12.1)
webmock (~> 3.23, >= 3.23.1)

BUNDLED WITH
2.5.9
2 changes: 1 addition & 1 deletion config/settings/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ wg_port: '51820'
wg_default_dns: '1.1.1.1'
wg_persistent_keepalive: 0
auth_token: '123-Ab'
webhooks_url: 'https://test.webhooks.com'
webhooks_url: 'https://test.webhooks.com/event'
28 changes: 12 additions & 16 deletions lib/webhooks/sender.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# frozen_string_literal: true

module Webhooks
# Sender +_+
# The class sends events in multi-threaded mode
class Sender
MAX_THREADS = 8
URL = Settings.webhooks_url

def initialize(events)
@events = events
@threads = []
end

def send_events # rubocop:disable Metrics/MethodLength
url = Settings.webhooks_url
return unless url

sorted_events = sort_events
threads = []
def send_events
return unless URL

sorted_events.each_value do |events|
sort_events.each_value do |events|
thread = Thread.new do
events.each do |event|
Client.new(url).send_payload(event)
Client.new(URL).send_payload(event)
end
end

Expand All @@ -29,9 +29,9 @@ def send_events # rubocop:disable Metrics/MethodLength

private

attr_reader :events
attr_reader :events, :threads

def sort_events # rubocop:disable Metrics/MethodLength
def sort_events
i = 1
result = {}

Expand All @@ -40,11 +40,7 @@ def sort_events # rubocop:disable Metrics/MethodLength
arr = [] if arr.nil?
arr << event
result[i] = arr
if i >= 5
i = 1
else
i += 1
end
i >= MAX_THREADS ? i = 1 : i += 1
end

result
Expand Down
14 changes: 11 additions & 3 deletions spec/lib/webhooks/aggregator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
before do
allow(WireGuard::StatGenerator).to receive_messages(show: wg_show_stub)
allow(WireGuard::ServerStat).to receive(:new)
create_conf_file('spec/fixtures/wg0_stat.json', wg_stat_path)
create_conf_file('spec/fixtures/empty_wg0_stat.json', wg_stat_path)
create_conf_file('spec/fixtures/wg0_events.json', wg_events_path)
end

Expand All @@ -15,11 +15,19 @@
FileUtils.rm_rf("#{Settings.wg_path}/wg0_stat.json")
end

let(:wg_show_stub) { File.read('spec/fixtures/stat_with_empty.txt') }
let(:wg_show_stub) { File.read('spec/fixtures/stat.txt') }
let(:wg_stat_path) { "#{Settings.wg_path}/wg0_stat.json" }
let(:wg_events_path) { "#{Settings.wg_path}/wg0_events.json" }

let(:expected_result) do
[
nil,
{ peer: 'hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=', event: 'connected' },
{ peer: 'bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=', event: 'disconnected' }
]
end

it 'returns correct events' do
expect(events).to eq([{ peer: 'bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=', event: 'disconnected' }])
expect(events).to eq(expected_result)
end
end
41 changes: 41 additions & 0 deletions spec/lib/webhooks/sender_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

RSpec.describe Webhooks::Sender do
subject(:send) { described_class.new(events).send_events }

let(:url) { Settings.webhooks_url }
let(:events) do
[
nil,
{ peer: 'f6e12b5b-ea10-47fb-9bcf-c73b2028dfb7', event: 'connected' },
{ peer: 'b954a686-5f45-4ffe-8793-900073d2a580', event: 'disconnected' },
{ peer: '2d675243-502f-46c9-8544-255e0f90576b', event: 'disconnected' },
{ peer: '455a9a69-d70b-4e4f-85e3-8c20516db0e8', event: 'connected' },
{ peer: 'afa4cebf-4991-4752-9925-95b61ee92513', event: 'disconnected' },
{ peer: 'f14b5e34-8f45-4adb-8128-00b74f63b693', event: 'connected' },
{ peer: '5c0fe354-40b8-4b86-b004-15e9a47cec1c', event: 'disconnected' },
{ peer: '15f4c68f-9cd0-4693-afd3-fa936e656f42', event: 'disconnected' },
{ peer: '003c6f7d-f575-4884-9fe1-6a95350fdb05', event: 'connected' },
{ peer: 'ea5d7970-7bd3-45fb-93f9-2f840aeedda0', event: 'disconnected' },
{ peer: '7a921f1e-4924-407c-8345-2f4fbf7945ae', event: 'connected' }
]
end

before do
events[1..].each do |event|
stub_request(:post, url).with(body: event.to_json).to_return(status: 200, body: '{}')
end

send
end

it 'sends the correct number of events' do
expect(WebMock).to have_requested(:post, url).times(events.size - 1)
end

it 'sends all events' do
events[1..].each do |event|
expect(WebMock).to have_requested(:post, url).with(body: event.to_json)
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def sentry?

require 'super_diff/rspec'
require 'rack/test'
require 'webmock/rspec'

RSpec.configure do |config|
config.example_status_persistence_file_path = '.rspec_status'
Expand Down

0 comments on commit f19a9b8

Please sign in to comment.