Skip to content

Commit

Permalink
fix: unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Oct 31, 2024
1 parent 3ceae95 commit 8beea44
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
7 changes: 5 additions & 2 deletions agglayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (

const errCodeAgglayerRateLimitExceeded int = -10007

var ErrAgglayerRateLimitExceeded = fmt.Errorf("agglayer rate limit exceeded")
var (
ErrAgglayerRateLimitExceeded = fmt.Errorf("agglayer rate limit exceeded")
jSONRPCCall = rpc.JSONRPCCall
)

type AggLayerClientGetClockConfiguration interface {
GetClockConfiguration() (*ClockConfiguration, error)
Expand Down Expand Up @@ -136,7 +139,7 @@ func (c *AggLayerClient) GetCertificateHeader(certificateHash common.Hash) (*Cer

// GetClockConfiguration returns the clock configuration of AggLayer
func (c *AggLayerClient) GetClockConfiguration() (*ClockConfiguration, error) {
response, err := rpc.JSONRPCCall(c.url, "interop_getClockConfiguration")
response, err := jSONRPCCall(c.url, "interop_getClockConfiguration")
if err != nil {
return nil, err
}
Expand Down
67 changes: 67 additions & 0 deletions agglayer/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package agglayer

import (
"fmt"
"testing"

"github.com/0xPolygon/cdk-rpc/rpc"
"github.com/stretchr/testify/require"
)

const (
testURL = "http://localhost:8080"
)

func TestGetClockConfigurationResponseWithError(t *testing.T) {
sut := NewAggLayerClient(testURL)
response := rpc.Response{
Error: &rpc.ErrorObject{},
}
jSONRPCCall = func(url, method string, params ...interface{}) (rpc.Response, error) {
return response, nil
}
clockConfig, err := sut.GetClockConfiguration()
require.Nil(t, clockConfig)
require.Error(t, err)
}

func TestGetClockConfigurationResponseBadJson(t *testing.T) {
sut := NewAggLayerClient(testURL)
response := rpc.Response{
Result: []byte(`{`),
}
jSONRPCCall = func(url, method string, params ...interface{}) (rpc.Response, error) {
return response, nil
}
clockConfig, err := sut.GetClockConfiguration()
require.Nil(t, clockConfig)
require.Error(t, err)
}

func TestGetClockConfigurationErrorResponse(t *testing.T) {
sut := NewAggLayerClient(testURL)

jSONRPCCall = func(url, method string, params ...interface{}) (rpc.Response, error) {
return rpc.Response{}, fmt.Errorf("unittest error")
}
clockConfig, err := sut.GetClockConfiguration()
require.Nil(t, clockConfig)
require.Error(t, err)
}

func TestGetClockConfigurationOkResponse(t *testing.T) {
sut := NewAggLayerClient(testURL)
response := rpc.Response{
Result: []byte(`{"epoch_duration": 1, "genesis_block": 1}`),
}
jSONRPCCall = func(url, method string, params ...interface{}) (rpc.Response, error) {
return response, nil
}
clockConfig, err := sut.GetClockConfiguration()
require.NotNil(t, clockConfig)
require.NoError(t, err)
require.Equal(t, ClockConfiguration{
EpochDuration: 1,
GenesisBlock: 1,
}, *clockConfig)
}
2 changes: 1 addition & 1 deletion config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ L2URL = "http://localhost:8123"
L1AggOracleURL = "http://test-aggoracle-l1:8545"
L2AggOracleURL = "http://test-aggoracle-l2:8545"
AggLayerURL = "https://agglayer-dev.polygon.technology"
AggLayerURL = "https://agglayer-dev.polygon.technology"
ForkId = 9
ContractVersions = "elderberry"
Expand Down

0 comments on commit 8beea44

Please sign in to comment.