Skip to content

Commit

Permalink
add comments and refactor variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyzags committed Jan 21, 2025
1 parent ea1fdf6 commit f8eda1f
Showing 1 changed file with 59 additions and 39 deletions.
98 changes: 59 additions & 39 deletions pkg/relayer/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,28 +557,30 @@ func TestRelayerProxy_Relays(t *testing.T) {
}
}

// RelayProxyPingAllSuite implements the suite to test the relay proxy ping
// application logic.
type RelayProxyPingAllSuite struct {
suite.Suite
relayerProxyBehavior []func(*testproxy.TestBehavior)
servicesConfigMap map[string]*config.RelayMinerServerConfig
supplierEndpoints map[string][]*sharedtypes.SupplierEndpoint
supplierOperatorPingAllKeyName string
defaultRelayMinerServerAddress string
defaultServiceName string
}

// TestRelayProxyPingAllSuite executes the RelayProxyPingAllSuite test suite.
func TestRelayProxyPingAllSuite(t *testing.T) {
suite.Run(t, new(RelayProxyPingAllSuite))
}

// SetupSuite setups a single default relayminer with one supplier.
// SetupSuite setups a single default relayminer with one supplier. The
// default relayminer will be reused in every subsequent tests in the
// suite.
func (t *RelayProxyPingAllSuite) SetupSuite() {
t.supplierOperatorPingAllKeyName = "supplierPingAllKeyName"
appPrivateKey := secp256k1.GenPrivKey()
t.defaultRelayMinerServerAddress = "127.0.0.1:8245"
t.defaultServiceName = "defaultService"
t.supplierEndpoints = map[string][]*sharedtypes.SupplierEndpoint{
t.defaultServiceName: {
defaultRelayMinerServerAddress := "127.0.0.1:8245"
defaultServiceName := "defaultService"
supplierEndpoints := map[string][]*sharedtypes.SupplierEndpoint{
defaultServiceName: {
{
Url: "http://supplier1pingall:8645",
RpcType: sharedtypes.RPCType_JSON_RPC,
Expand All @@ -587,12 +589,12 @@ func (t *RelayProxyPingAllSuite) SetupSuite() {
}

t.servicesConfigMap = map[string]*config.RelayMinerServerConfig{
t.defaultRelayMinerServerAddress: {
defaultRelayMinerServerAddress: {
ServerType: config.RelayMinerServerTypeHTTP,
ListenAddress: t.defaultRelayMinerServerAddress,
ListenAddress: defaultRelayMinerServerAddress,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
t.defaultServiceName: {
ServiceId: t.defaultServiceName,
defaultServiceName: {
ServiceId: defaultServiceName,
ServerType: config.RelayMinerServerTypeHTTP,
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
BackendUrl: &url.URL{
Expand All @@ -612,13 +614,15 @@ func (t *RelayProxyPingAllSuite) SetupSuite() {
t.relayerProxyBehavior = []func(*testproxy.TestBehavior){
testproxy.WithRelayerProxyDependenciesForBlockHeight(t.supplierOperatorPingAllKeyName, 1),
testproxy.WithServicesConfigMap(t.servicesConfigMap),
testproxy.WithDefaultSupplier(t.supplierOperatorPingAllKeyName, t.supplierEndpoints),
testproxy.WithDefaultSupplier(t.supplierOperatorPingAllKeyName, supplierEndpoints),
testproxy.WithDefaultApplication(appPrivateKey),
testproxy.WithDefaultSessionSupplier(t.supplierOperatorPingAllKeyName, t.defaultServiceName, appPrivateKey),
testproxy.WithDefaultSessionSupplier(t.supplierOperatorPingAllKeyName, defaultServiceName, appPrivateKey),
testproxy.WithRelayMeter(),
}
}

// TestOKPingAllWithSingleRelayServer reuses the default relayminer with one
// supplier to test the relayproxy.PingAll method.
func (t *RelayProxyPingAllSuite) TestOKPingAllWithSingleRelayServer() {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
Expand All @@ -637,7 +641,6 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllWithSingleRelayServer() {
if http.ErrServerClosed != err {
require.NoError(t.T(), err)
}

}()

// waiting for relayer proxy to start
Expand All @@ -651,12 +654,14 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllWithSingleRelayServer() {
require.NoError(t.T(), err)
}

// TestOKPingAllWithMultipleRelayServers reuses default relayminer and
// instantiates an additional one to test the connectivity.
func (t *RelayProxyPingAllSuite) TestOKPingAllWithMultipleRelayServers() {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

secondRelayMinerAddr := "127.0.0.1:8246"
secondServiceName := "secondservice"
secondServiceAddr := "127.0.0.1:8246"

// adding supplier endpoint.
supplierEndpoints := map[string][]*sharedtypes.SupplierEndpoint{
Expand All @@ -669,9 +674,9 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllWithMultipleRelayServers() {
}

servicesConfigMap := map[string]*config.RelayMinerServerConfig{
secondServiceAddr: {
secondRelayMinerAddr: {
ServerType: config.RelayMinerServerTypeHTTP,
ListenAddress: secondServiceAddr,
ListenAddress: secondRelayMinerAddr,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
secondServiceName: {
ServiceId: secondServiceName,
Expand All @@ -698,7 +703,8 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllWithMultipleRelayServers() {

testBehavoirs := testproxy.NewRelayerProxyTestBehavior(ctx, t.T(), relayProxyBehavior...)

// copying default service config map
// copying the default relayminer in the test service config
// map for the relay proxy.
for k, v := range t.servicesConfigMap {
servicesConfigMap[k] = v
}
Expand Down Expand Up @@ -728,12 +734,14 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllWithMultipleRelayServers() {
require.NoError(t.T(), err)
}

// TestNOKPingAllWithPartialFailureAtStartup test the connectivity for multiple
// relayminer with a partial sucess/failure at startup.
func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAtStartup() {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

failingRelayMinerAddr := "127.0.0.1:8247"
failingServiceName := "failingservice"
failingServiceAddr := "127.0.0.1:8247"

supplierEndpoints := map[string][]*sharedtypes.SupplierEndpoint{
failingServiceName: {
Expand All @@ -745,8 +753,8 @@ func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAtStartup() {
}

servicesConfigMap := map[string]*config.RelayMinerServerConfig{
failingServiceAddr: &config.RelayMinerServerConfig{
ListenAddress: failingServiceAddr,
failingRelayMinerAddr: &config.RelayMinerServerConfig{
ListenAddress: failingRelayMinerAddr,
ServerType: config.RelayMinerServerTypeHTTP,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
failingServiceName: &config.RelayMinerSupplierConfig{
Expand Down Expand Up @@ -774,7 +782,8 @@ func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAtStartup() {

test := testproxy.NewRelayerProxyTestBehavior(ctx, t.T(), relayProxyBehavior...)

// copying services config maps from default values
// copying the default relayminer in the test service config
// map for the relay proxy.
for k, v := range t.servicesConfigMap {
servicesConfigMap[k] = v
}
Expand All @@ -786,9 +795,12 @@ func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAtStartup() {
)
require.NoError(t.T(), err)

// we are explicitly shutting down a supplier to simulate a
// failure while testing the connectivity at startup.
err = test.ShutdownServiceID(failingServiceName)
require.NoError(t.T(), err)

// testing connectivity at startup
err = rp.Start(ctx)
require.Error(t.T(), err)
require.True(t.T(), strings.Contains(err.Error(), "connection refused"))
Expand All @@ -797,12 +809,14 @@ func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAtStartup() {
require.NoError(t.T(), err)
}

// TestNOKPingAllWithPartialFailureAfterStartup test the connectivity for multiple
// relayminer with a partial sucess/failure at runtime.
func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAfterStartup() {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

failingRelayMinerAddr := "127.0.0.1:8248"
failingServiceName := "faillingservice"
failingServiceAddr := "127.0.0.1:8248"

supplierEndpoints := map[string][]*sharedtypes.SupplierEndpoint{
failingServiceName: {
Expand All @@ -814,8 +828,8 @@ func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAfterStartup()
}

servicesConfigMap := map[string]*config.RelayMinerServerConfig{
failingServiceAddr: &config.RelayMinerServerConfig{
ListenAddress: failingServiceAddr,
failingRelayMinerAddr: &config.RelayMinerServerConfig{
ListenAddress: failingRelayMinerAddr,
ServerType: config.RelayMinerServerTypeHTTP,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
failingServiceName: &config.RelayMinerSupplierConfig{
Expand Down Expand Up @@ -843,7 +857,8 @@ func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAfterStartup()

test := testproxy.NewRelayerProxyTestBehavior(ctx, t.T(), relayProxyBehavior...)

// copying services config maps from default values
// copying the default relayminer in the test service config
// map for the relay proxy.
for k, v := range t.servicesConfigMap {
servicesConfigMap[k] = v
}
Expand All @@ -866,6 +881,8 @@ func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAfterStartup()
// and perform ping request.
time.Sleep(100 * time.Millisecond)

// we are explicitly shutting down a supplier to simulate an error
// while testing the connectivity.
err = test.ShutdownServiceID(failingServiceName)
require.NoError(t.T(), err)

Expand All @@ -877,6 +894,8 @@ func (t *RelayProxyPingAllSuite) TestNOKPingAllWithPartialFailureAfterStartup()
require.NoError(t.T(), err)
}

// TestOKPingAllDifferentEndpoint test the connectivity with different type of
// endpoints (ipv6, domain name).
func (t *RelayProxyPingAllSuite) TestOKPingAllDifferentEndpoint() {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
Expand All @@ -892,20 +911,20 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllDifferentEndpoint() {
srv.PatchNet(net.DefaultResolver)
defer mockdns.UnpatchNet(net.DefaultResolver)

relayminerZoneServiceName := "exampleservice.org"
relayminerZoneServiceAddr := "exampleservice.org:8249"
relayminerDomainNameAddr := "exampleservice.org:8249"
domainNameServiceName := "exampleservice.org"

relayminerIPV6ServiceName := "ipv6service"
relayminerIPV6ServiceAddr := "localhost:8250"
IPV6ServiceName := "ipv6service"

supplierEndpoints := map[string][]*sharedtypes.SupplierEndpoint{
relayminerZoneServiceName: {
domainNameServiceName: {
{
Url: "http://exampleservice.org:8649",
RpcType: sharedtypes.RPCType_JSON_RPC,
},
},
relayminerIPV6ServiceName: {
IPV6ServiceName: {
{
Url: "http://ipv6service:8650",
RpcType: sharedtypes.RPCType_JSON_RPC,
Expand All @@ -914,13 +933,13 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllDifferentEndpoint() {
}

servicesConfigMap := map[string]*config.RelayMinerServerConfig{
relayminerZoneServiceAddr: &config.RelayMinerServerConfig{
ListenAddress: relayminerZoneServiceAddr,
relayminerDomainNameAddr: &config.RelayMinerServerConfig{
ListenAddress: relayminerDomainNameAddr,
ServerType: config.RelayMinerServerTypeHTTP,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
relayminerZoneServiceName: &config.RelayMinerSupplierConfig{
domainNameServiceName: &config.RelayMinerSupplierConfig{
ServerType: config.RelayMinerServerTypeHTTP,
ServiceId: relayminerZoneServiceName,
ServiceId: domainNameServiceName,
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
BackendUrl: &url.URL{
Scheme: "http",
Expand All @@ -938,9 +957,9 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllDifferentEndpoint() {
ListenAddress: relayminerIPV6ServiceAddr,
ServerType: config.RelayMinerServerTypeHTTP,
SupplierConfigsMap: map[string]*config.RelayMinerSupplierConfig{
relayminerIPV6ServiceName: &config.RelayMinerSupplierConfig{
IPV6ServiceName: &config.RelayMinerSupplierConfig{
ServerType: config.RelayMinerServerTypeHTTP,
ServiceId: relayminerIPV6ServiceName,
ServiceId: IPV6ServiceName,
ServiceConfig: &config.RelayMinerSupplierServiceConfig{
BackendUrl: &url.URL{
Scheme: "http",
Expand All @@ -963,7 +982,8 @@ func (t *RelayProxyPingAllSuite) TestOKPingAllDifferentEndpoint() {

test := testproxy.NewRelayerProxyTestBehavior(ctx, t.T(), relayProxyBehavior...)

// copying services config maps from default values
// copying the default relayminer in the test service config
// map for the relay proxy.
for k, v := range t.servicesConfigMap {
servicesConfigMap[k] = v
}
Expand Down

0 comments on commit f8eda1f

Please sign in to comment.