github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/nat/event/sender_test.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package event
    19  
    20  import (
    21  	"errors"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  type mockMetricsSender struct {
    28  	mockResponse error
    29  
    30  	successSent   bool
    31  	failErrorSent error
    32  	stageSent     string
    33  	gatewaysSent  []map[string]string
    34  }
    35  
    36  func buildMockMetricsSender(mockResponse error) *mockMetricsSender {
    37  	return &mockMetricsSender{mockResponse: mockResponse, successSent: false}
    38  }
    39  
    40  func (sender *mockMetricsSender) SendNATMappingSuccessEvent(id, stage string, gateways []map[string]string) {
    41  	sender.successSent = true
    42  	sender.stageSent = stage
    43  	sender.gatewaysSent = gateways
    44  }
    45  
    46  func (sender *mockMetricsSender) SendNATMappingFailEvent(id, stage string, gateways []map[string]string, err error) {
    47  	sender.failErrorSent = err
    48  	sender.stageSent = stage
    49  	sender.gatewaysSent = gateways
    50  }
    51  
    52  type mockIPResolver struct {
    53  	mockIp  string
    54  	mockErr error
    55  }
    56  
    57  func (resolver *mockIPResolver) GetPublicIP() (string, error) {
    58  	return resolver.mockIp, resolver.mockErr
    59  }
    60  
    61  var (
    62  	mockGateways = []map[string]string{
    63  		{"test": "test"},
    64  	}
    65  	mockGatewayLoader = func() []map[string]string {
    66  		return mockGateways
    67  	}
    68  )
    69  
    70  func Test_EventsSender_ConsumeNATEvent_SendsSuccessEvent(t *testing.T) {
    71  	mockMetricsSender := buildMockMetricsSender(nil)
    72  	mockIPResolver := mockIPResolver{mockIp: "1st ip"}
    73  	sender := NewSender(mockMetricsSender, mockIPResolver.GetPublicIP, mockGatewayLoader)
    74  
    75  	sender.consumeNATEvent(Event{Stage: "hole_punching", Successful: true})
    76  
    77  	assert.True(t, mockMetricsSender.successSent)
    78  	assert.Equal(t, "hole_punching", mockMetricsSender.stageSent)
    79  	assert.Equal(t, mockGateways, mockMetricsSender.gatewaysSent)
    80  }
    81  
    82  func Test_EventsSender_ConsumeNATEvent_WithSameIp_DoesNotSendSuccessEventAgain(t *testing.T) {
    83  	mockMetricsSender := buildMockMetricsSender(nil)
    84  	mockIPResolver := mockIPResolver{mockIp: "1st ip"}
    85  	sender := NewSender(mockMetricsSender, mockIPResolver.GetPublicIP, mockGatewayLoader)
    86  
    87  	sender.consumeNATEvent(Event{Successful: true})
    88  
    89  	mockMetricsSender.successSent = false
    90  
    91  	sender.consumeNATEvent(Event{Successful: true})
    92  	assert.False(t, mockMetricsSender.successSent)
    93  	assert.Equal(t, mockGateways, mockMetricsSender.gatewaysSent)
    94  }
    95  
    96  func Test_EventsSender_ConsumeNATEvent_WithDifferentIP_SendsSuccessEventAgain(t *testing.T) {
    97  	mockMetricsSender := buildMockMetricsSender(nil)
    98  	mockIPResolver := &mockIPResolver{mockIp: "1st ip"}
    99  	sender := NewSender(mockMetricsSender, mockIPResolver.GetPublicIP, mockGatewayLoader)
   100  
   101  	sender.consumeNATEvent(Event{Successful: true})
   102  
   103  	mockMetricsSender.successSent = false
   104  
   105  	mockIPResolver.mockIp = "2nd ip"
   106  	sender.consumeNATEvent(Event{Successful: true})
   107  	assert.True(t, mockMetricsSender.successSent)
   108  	assert.Equal(t, mockGateways, mockMetricsSender.gatewaysSent)
   109  }
   110  
   111  func Test_EventsSender_ConsumeNATEvent_WhenIPResolverFails_DoesNotSendEvent(t *testing.T) {
   112  	mockMetricsSender := buildMockMetricsSender(nil)
   113  	mockIPResolver := &mockIPResolver{mockErr: errors.New("mock error")}
   114  	sender := NewSender(mockMetricsSender, mockIPResolver.GetPublicIP, mockGatewayLoader)
   115  
   116  	sender.consumeNATEvent(Event{Successful: true})
   117  
   118  	assert.False(t, mockMetricsSender.successSent)
   119  	assert.Nil(t, mockMetricsSender.gatewaysSent)
   120  }
   121  
   122  func Test_EventsSender_ConsumeNATEvent_SendsFailureEvent(t *testing.T) {
   123  	mockMetricsSender := buildMockMetricsSender(nil)
   124  	mockIPResolver := mockIPResolver{mockIp: "1st ip"}
   125  	sender := NewSender(mockMetricsSender, mockIPResolver.GetPublicIP, mockGatewayLoader)
   126  
   127  	testErr := errors.New("test error")
   128  	sender.consumeNATEvent(Event{Stage: "hole_punching", Successful: false, Error: testErr})
   129  
   130  	assert.Equal(t, testErr, mockMetricsSender.failErrorSent)
   131  	assert.Equal(t, "hole_punching", mockMetricsSender.stageSent)
   132  	assert.Equal(t, mockGateways, mockMetricsSender.gatewaysSent)
   133  }
   134  
   135  func Test_EventsSender_ConsumeNATEvent_WithFailuresOfDifferentStages_SendsBothEvents(t *testing.T) {
   136  	mockMetricsSender := buildMockMetricsSender(nil)
   137  	mockIPResolver := &mockIPResolver{mockIp: "1st ip"}
   138  	sender := NewSender(mockMetricsSender, mockIPResolver.GetPublicIP, mockGatewayLoader)
   139  
   140  	testErr1 := errors.New("test error 1")
   141  	sender.consumeNATEvent(Event{Successful: false, Error: testErr1, Stage: "test 1"})
   142  	testErr2 := errors.New("test error 2")
   143  	sender.consumeNATEvent(Event{Successful: false, Error: testErr2, Stage: "test 2"})
   144  
   145  	assert.Equal(t, testErr2, mockMetricsSender.failErrorSent)
   146  	assert.Equal(t, mockGateways, mockMetricsSender.gatewaysSent)
   147  }
   148  
   149  func Test_EventsSender_ConsumeNATEvent_WithSuccessAndFailureOnSameIp_SendsBothEvents(t *testing.T) {
   150  	mockMetricsSender := buildMockMetricsSender(nil)
   151  	mockIPResolver := &mockIPResolver{mockIp: "1st ip"}
   152  	sender := NewSender(mockMetricsSender, mockIPResolver.GetPublicIP, mockGatewayLoader)
   153  
   154  	sender.consumeNATEvent(Event{Successful: true})
   155  	testErr := errors.New("test error")
   156  	sender.consumeNATEvent(Event{Successful: false, Error: testErr})
   157  
   158  	assert.True(t, mockMetricsSender.successSent)
   159  	assert.Equal(t, mockMetricsSender.failErrorSent, testErr)
   160  	assert.Equal(t, mockGateways, mockMetricsSender.gatewaysSent)
   161  }