From 28a07160bfbb652ffe4d3e426ca6eea22e0253b7 Mon Sep 17 00:00:00 2001 From: Matthias <5011972+fasmat@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:16:56 +0000 Subject: [PATCH] Cleanup --- api/grpcserver/admin_service_test.go | 8 ++--- api/grpcserver/grpcserver_json_test.go | 41 +++++++++++++++----------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/api/grpcserver/admin_service_test.go b/api/grpcserver/admin_service_test.go index 5a9e0d9d2b9..2df5b8ff0dc 100644 --- a/api/grpcserver/admin_service_test.go +++ b/api/grpcserver/admin_service_test.go @@ -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{ @@ -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) @@ -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"), diff --git a/api/grpcserver/grpcserver_json_test.go b/api/grpcserver/grpcserver_json_test.go index f138a7407aa..1021ed388fa 100644 --- a/api/grpcserver/grpcserver_json_test.go +++ b/api/grpcserver/grpcserver_json_test.go @@ -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) }