github.com/status-im/status-go@v1.1.0/services/ext/mailrequests_test.go (about)

     1  package ext
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/status-im/status-go/eth-node/types"
    11  )
    12  
    13  var (
    14  	testHash = types.Hash{0x01}
    15  )
    16  
    17  func TestMailRequestMonitorSuite(t *testing.T) {
    18  	suite.Run(t, new(MailRequestMonitorSuite))
    19  }
    20  
    21  type MailRequestMonitorSuite struct {
    22  	suite.Suite
    23  
    24  	monitor *MailRequestMonitor
    25  }
    26  
    27  func (s *MailRequestMonitorSuite) SetupTest() {
    28  	s.monitor = &MailRequestMonitor{
    29  		cache:            map[types.Hash]EnvelopeState{},
    30  		requestsRegistry: NewRequestsRegistry(0),
    31  	}
    32  }
    33  
    34  func (s *MailRequestMonitorSuite) TestRequestCompleted() {
    35  	mock := NewHandlerMock(1)
    36  	s.monitor.handler = mock
    37  	s.monitor.cache[testHash] = MailServerRequestSent
    38  	s.monitor.handleEvent(types.EnvelopeEvent{
    39  		Event: types.EventMailServerRequestCompleted,
    40  		Hash:  testHash,
    41  		Data:  &types.MailServerResponse{},
    42  	})
    43  	select {
    44  	case requestID := <-mock.requestsCompleted:
    45  		s.Equal(testHash, requestID)
    46  		s.NotContains(s.monitor.cache, testHash)
    47  	case <-time.After(10 * time.Second):
    48  		s.Fail("timed out while waiting for a request to be completed")
    49  	}
    50  }
    51  
    52  func (s *MailRequestMonitorSuite) TestRequestFailed() {
    53  	mock := NewHandlerMock(1)
    54  	s.monitor.handler = mock
    55  	s.monitor.cache[testHash] = MailServerRequestSent
    56  	s.monitor.handleEvent(types.EnvelopeEvent{
    57  		Event: types.EventMailServerRequestCompleted,
    58  		Hash:  testHash,
    59  		Data:  &types.MailServerResponse{Error: errors.New("test error")},
    60  	})
    61  	select {
    62  	case requestID := <-mock.requestsFailed:
    63  		s.Equal(testHash, requestID)
    64  		s.NotContains(s.monitor.cache, testHash)
    65  	case <-time.After(10 * time.Second):
    66  		s.Fail("timed out while waiting for a request to be failed")
    67  	}
    68  }
    69  
    70  func (s *MailRequestMonitorSuite) TestRequestExpiration() {
    71  	mock := NewHandlerMock(1)
    72  	s.monitor.handler = mock
    73  	s.monitor.cache[testHash] = MailServerRequestSent
    74  	s.monitor.handleEvent(types.EnvelopeEvent{
    75  		Event: types.EventMailServerRequestExpired,
    76  		Hash:  testHash,
    77  	})
    78  	select {
    79  	case requestID := <-mock.requestsExpired:
    80  		s.Equal(testHash, requestID)
    81  		s.NotContains(s.monitor.cache, testHash)
    82  	case <-time.After(10 * time.Second):
    83  		s.Fail("timed out while waiting for request expiration")
    84  	}
    85  }