Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handler integration test #12100

Merged
Prev Previous commit
cleanup
  • Loading branch information
amirylm committed Feb 25, 2024
commit 9ee15689de9e9b7530bb678ba6619814b87f110b
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package mercury

import (
"net/http"
"net/http/httptest"
"sync/atomic"
)

type MercuryEndpointMock interface {
URL() string
Username() string
Password() string
CallCount() int
RegisterHandler(http.HandlerFunc)
}

var _ MercuryEndpointMock = &SimulatedMercuryServer{}

var notFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})

type SimulatedMercuryServer struct {
server *httptest.Server
handler http.HandlerFunc

callCounter atomic.Int32
}

func NewSimulatedMercuryServer() *SimulatedMercuryServer {
srv := &SimulatedMercuryServer{
handler: notFoundHandler,
callCounter: atomic.Int32{},
}

srv.server = httptest.NewUnstartedServer(srv)

return srv
}

func (ms *SimulatedMercuryServer) URL() string {
return ms.server.URL
}

func (ms *SimulatedMercuryServer) Username() string {
return "username1"
}

func (ms *SimulatedMercuryServer) Password() string {
return "password1"
}

func (ms *SimulatedMercuryServer) CallCount() int {
return int(ms.callCounter.Load())
}

func (ms *SimulatedMercuryServer) RegisterHandler(h http.HandlerFunc) {
ms.handler = h
}

func (ms *SimulatedMercuryServer) Start() {
ms.server.Start()
}

func (ms *SimulatedMercuryServer) Stop() {
ms.server.Close()
}

func (ms *SimulatedMercuryServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ms.callCounter.Add(1)
ms.handler.ServeHTTP(w, r)
}
19 changes: 5 additions & 14 deletions core/services/ocr2/plugins/ocr2keeper/integration_21_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/streams"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"
)
Expand Down Expand Up @@ -417,9 +418,7 @@ func TestIntegration_KeeperPluginLogUpkeep_ErrHandler(t *testing.T) {

upkeepCount := 10

respTimeout := -1
errResponses := []int{
// respTimeout, // TODO: uncomment once mercuryServer is fixed
http.StatusUnauthorized,
http.StatusBadRequest,
http.StatusInternalServerError,
Expand All @@ -429,15 +428,7 @@ func TestIntegration_KeeperPluginLogUpkeep_ErrHandler(t *testing.T) {
if i < len(errResponses) {
resp = errResponses[i]
}
switch resp {
case http.StatusNotFound, http.StatusInternalServerError:
// TODO: uncomment once mercuryServer is fixed
// in case we got a 404 or 500, wait a bit to simulate real world
// time.Sleep(time.Duration(rand.Intn(2500)) * time.Millisecond)
case respTimeout: // mercury server timeout
time.Sleep(30 * time.Second)
resp = http.StatusNotFound
default:
if resp == 0 {
resp = http.StatusNotFound
}
return resp, nil
Expand Down Expand Up @@ -488,7 +479,7 @@ func TestIntegration_KeeperPluginLogUpkeep_ErrHandler(t *testing.T) {
done()
}

func startMercuryServer(t *testing.T, mercuryServer *SimulatedMercuryServer, responder func(i int) (int, []byte)) {
func startMercuryServer(t *testing.T, mercuryServer *mercury.SimulatedMercuryServer, responder func(i int) (int, []byte)) {
i := atomic.Int32{}
mercuryServer.RegisterHandler(func(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
Expand Down Expand Up @@ -595,9 +586,9 @@ func listenPerformed(t *testing.T, backend *backends.SimulatedBackend, registry
return listenPerformedN(t, backend, registry, ids, startBlock, 0)
}

func setupNodes(t *testing.T, nodeKeys [5]ethkey.KeyV2, registry *iregistry21.IKeeperRegistryMaster, backend *backends.SimulatedBackend, usr *bind.TransactOpts) ([]Node, *SimulatedMercuryServer) {
func setupNodes(t *testing.T, nodeKeys [5]ethkey.KeyV2, registry *iregistry21.IKeeperRegistryMaster, backend *backends.SimulatedBackend, usr *bind.TransactOpts) ([]Node, *mercury.SimulatedMercuryServer) {
lggr := logger.TestLogger(t)
mServer := NewSimulatedMercuryServer()
mServer := mercury.NewSimulatedMercuryServer()
mServer.Start()

// Setup bootstrap + oracle nodes
Expand Down
83 changes: 6 additions & 77 deletions core/services/ocr2/plugins/ocr2keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"encoding/json"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -57,6 +54,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/keystest"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ocr2key"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/validate"
"github.com/smartcontractkit/chainlink/v2/core/services/ocrbootstrap"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm"
Expand Down Expand Up @@ -112,7 +110,7 @@ func setupNode(
nodeKey ethkey.KeyV2,
backend *backends.SimulatedBackend,
p2pV2Bootstrappers []commontypes.BootstrapperLocator,
mercury MercuryEndpoint,
mercury mercury.MercuryEndpointMock,
) (chainlink.Application, string, common.Address, ocr2key.KeyBundle) {
p2pKey := keystest.NewP2PKeyV2(t)
p2paddresses := []string{fmt.Sprintf("127.0.0.1:%d", port)}
Expand Down Expand Up @@ -236,7 +234,7 @@ func TestIntegration_KeeperPluginBasic(t *testing.T) {

// Setup bootstrap + oracle nodes
bootstrapNodePort := freeport.GetOne(t)
appBootstrap, bootstrapPeerID, bootstrapTransmitter, bootstrapKb := setupNode(t, bootstrapNodePort, nodeKeys[0], backend, nil, NewSimulatedMercuryServer())
appBootstrap, bootstrapPeerID, bootstrapTransmitter, bootstrapKb := setupNode(t, bootstrapNodePort, nodeKeys[0], backend, nil, mercury.NewSimulatedMercuryServer())
bootstrapNode := Node{
appBootstrap, bootstrapTransmitter, bootstrapKb,
}
Expand All @@ -250,7 +248,7 @@ func TestIntegration_KeeperPluginBasic(t *testing.T) {
app, peerID, transmitter, kb := setupNode(t, ports[i], nodeKeys[i+1], backend, []commontypes.BootstrapperLocator{
// Supply the bootstrap IP and port as a V2 peer address
{PeerID: bootstrapPeerID, Addrs: []string{fmt.Sprintf("127.0.0.1:%d", bootstrapNodePort)}},
}, NewSimulatedMercuryServer())
}, mercury.NewSimulatedMercuryServer())

nodes = append(nodes, Node{
app, transmitter, kb,
Expand Down Expand Up @@ -488,7 +486,7 @@ func TestIntegration_KeeperPluginForwarderEnabled(t *testing.T) {
effectiveTransmitters := make([]common.Address, 0)
// Setup bootstrap + oracle nodes
bootstrapNodePort := freeport.GetOne(t)
appBootstrap, bootstrapPeerID, bootstrapTransmitter, bootstrapKb := setupNode(t, bootstrapNodePort, nodeKeys[0], backend, nil, NewSimulatedMercuryServer())
appBootstrap, bootstrapPeerID, bootstrapTransmitter, bootstrapKb := setupNode(t, bootstrapNodePort, nodeKeys[0], backend, nil, mercury.NewSimulatedMercuryServer())

bootstrapNode := Node{
appBootstrap, bootstrapTransmitter, bootstrapKb,
Expand All @@ -503,7 +501,7 @@ func TestIntegration_KeeperPluginForwarderEnabled(t *testing.T) {
app, peerID, transmitter, kb := setupNode(t, ports[i], nodeKeys[i+1], backend, []commontypes.BootstrapperLocator{
// Supply the bootstrap IP and port as a V2 peer address
{PeerID: bootstrapPeerID, Addrs: []string{fmt.Sprintf("127.0.0.1:%d", bootstrapNodePort)}},
}, NewSimulatedMercuryServer())
}, mercury.NewSimulatedMercuryServer())
nodeForwarder := setupForwarderForNode(t, app, sergey, backend, transmitter, linkAddr)
effectiveTransmitters = append(effectiveTransmitters, nodeForwarder)

Expand Down Expand Up @@ -711,72 +709,3 @@ func TestFilterNamesFromSpec20(t *testing.T) {
_, err = ocr2keeper.FilterNamesFromSpec20(spec)
require.ErrorContains(t, err, "not a valid EIP55 formatted address")
}

// ------- below this line could be added to a test helpers package
type MercuryEndpoint interface {
URL() string
Username() string
Password() string
CallCount() int
RegisterHandler(http.HandlerFunc)
}

type SimulatedMercuryServer struct {
server *httptest.Server
handler http.HandlerFunc

mu sync.RWMutex
callCount int
}

func NewSimulatedMercuryServer() *SimulatedMercuryServer {
srv := &SimulatedMercuryServer{
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
},
}

srv.server = httptest.NewUnstartedServer(srv)

return srv
}

func (ms *SimulatedMercuryServer) URL() string {
return ms.server.URL
}

func (ms *SimulatedMercuryServer) Username() string {
return "username1"
}

func (ms *SimulatedMercuryServer) Password() string {
return "password1"
}

func (ms *SimulatedMercuryServer) CallCount() int {
ms.mu.RLock()
defer ms.mu.RUnlock()

return ms.callCount
}

func (ms *SimulatedMercuryServer) RegisterHandler(h http.HandlerFunc) {
ms.handler = h
}

func (ms *SimulatedMercuryServer) Start() {
ms.server.Start()
}

func (ms *SimulatedMercuryServer) Stop() {
ms.server.Close()
}

func (ms *SimulatedMercuryServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ms.mu.Lock()
defer ms.mu.Unlock()

ms.callCount++

ms.handler.ServeHTTP(w, r)
}
Loading