Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Oct 13, 2023
1 parent 48a64f2 commit 28a0716
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
8 changes: 4 additions & 4 deletions api/grpcserver/admin_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

const snapshot uint32 = 15

func newatx(tb testing.TB, db *sql.Database) {
func newAtx(tb testing.TB, db *sql.Database) {
atx := &types.ActivationTx{
InnerActivationTx: types.InnerActivationTx{
NIPostChallenge: types.NIPostChallenge{
Expand All @@ -32,8 +32,8 @@ func newatx(tb testing.TB, db *sql.Database) {
},
}
atx.SetID(types.RandomATXID())
vrfnonce := types.VRFPostIndex(11)
atx.VRFNonce = &vrfnonce
vrfNonce := types.VRFPostIndex(11)
atx.VRFNonce = &vrfNonce
atx.SmesherID = types.BytesToNodeID(types.RandomBytes(20))
atx.NodeID = &atx.SmesherID
atx.SetEffectiveNumUnits(atx.NumUnits)
Expand All @@ -45,7 +45,7 @@ func newatx(tb testing.TB, db *sql.Database) {

func createMesh(tb testing.TB, db *sql.Database) {
for i := 0; i < 10; i++ {
newatx(tb, db)
newAtx(tb, db)
}
acct := &types.Account{
Layer: types.LayerID(0), Address: types.Address{1, 1}, NextNonce: 1, Balance: 1300, TemplateAddress: &types.Address{2}, State: []byte("state10"),
Expand Down
41 changes: 24 additions & 17 deletions api/grpcserver/grpcserver_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,53 @@ func launchJsonServer(tb testing.TB, services ...ServiceAPI) (Config, func()) {
return cfg, func() { assert.NoError(tb, jsonService.Shutdown(context.Background())) }
}

func callEndpoint(t *testing.T, url string, payload []byte) ([]byte, int) {
resp, err := http.Post(url, "application/json", bytes.NewReader(payload))
require.NoError(t, err)
require.Equal(t, "application/json", resp.Header.Get("Content-Type"))
buf, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
func callEndpoint(ctx context.Context, tb testing.TB, url string, body []byte) ([]byte, int) {
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
require.NoError(tb, err)
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
require.NoError(tb, err)
require.Equal(tb, "application/json", resp.Header.Get("Content-Type"))
buf, err := io.ReadAll(resp.Body)
require.NoError(tb, err)
require.NoError(tb, resp.Body.Close())
return buf, resp.StatusCode
}

func TestJsonApi(t *testing.T) {
const message = "hello world!"
const layerDuration = 10 * time.Second
const layerAvgSize = 10
const txsPerProposal = 99

ctrl := gomock.NewController(t)
syncer := NewMocksyncer(ctrl)
syncer.EXPECT().IsSynced(gomock.Any()).Return(false).AnyTimes()
ctrl, ctx := gomock.WithContext(context.Background(), t)
peerCounter := NewMockpeerCounter(ctrl)
meshAPIMock := NewMockmeshAPI(ctrl)
genTime := NewMockgenesisTimeAPI(ctrl)
genesis := time.Unix(genTimeUnix, 0)
genTime.EXPECT().GenesisTime().Return(genesis)
syncer := NewMocksyncer(ctrl)
conStateAPI := NewMockconservativeState(ctrl)
svc1 := NewNodeService(peerCounter, meshAPIMock, genTime, syncer, "v0.0.0", "cafebabe")
svc2 := NewMeshService(datastore.NewCachedDB(sql.InMemory(), logtest.New(t)), meshAPIMock, conStateAPI, genTime, layersPerEpoch, types.Hash20{}, layerDuration, layerAvgSize, txsPerProposal)
svc2 := NewMeshService(datastore.NewCachedDB(sql.InMemory(), logtest.New(t)), meshAPIMock, conStateAPI, genTime, 5, types.Hash20{}, layerDuration, layerAvgSize, txsPerProposal)
cfg, cleanup := launchJsonServer(t, svc1, svc2)
t.Cleanup(cleanup)
time.Sleep(time.Second)

// generate request payload (api input params)
const message = "hello world!"
payload, err := protojson.Marshal(&pb.EchoRequest{Msg: &pb.SimpleString{Value: message}})
require.NoError(t, err)
respBody, respStatus := callEndpoint(t, fmt.Sprintf("http://%s/v1/node/echo", cfg.JSONListener), payload)
respBody, respStatus := callEndpoint(ctx, t, fmt.Sprintf("http://%s/v1/node/echo", cfg.JSONListener), payload)
require.Equal(t, http.StatusOK, respStatus)
var msg pb.EchoResponse
require.NoError(t, protojson.Unmarshal(respBody, &msg))
require.Equal(t, message, msg.Msg.Value)

// Test MeshService
respBody2, respStatus2 := callEndpoint(t, fmt.Sprintf("http://%s/v1/mesh/genesistime", cfg.JSONListener), nil)
now := time.Now()
genTime.EXPECT().GenesisTime().Return(now)
respBody2, respStatus2 := callEndpoint(ctx, t, fmt.Sprintf("http://%s/v1/mesh/genesistime", cfg.JSONListener), nil)
require.Equal(t, http.StatusOK, respStatus2)
var msg2 pb.GenesisTimeResponse
require.NoError(t, protojson.Unmarshal(respBody2, &msg2))
require.Equal(t, uint64(genesis.Unix()), msg2.Unixtime.Value)
require.Equal(t, uint64(now.Unix()), msg2.Unixtime.Value)
}

0 comments on commit 28a0716

Please sign in to comment.