33package app
44
55import (
6- "context"
76 "encoding/hex"
87 "io/ioutil"
9- "net"
108 "os"
119 "path"
1210 "strings"
@@ -15,75 +13,17 @@ import (
1513 "time"
1614
1715 "github.com/cosmos/cosmos-sdk/server"
18- "github.com/dfinance/dvm-proto/go/vm_grpc"
1916 "github.com/stretchr/testify/require"
20- "google.golang.org/grpc"
21- "google.golang.org/grpc/codes"
22- grpcStatus "google.golang.org/grpc/status"
2317
2418 "github.com/dfinance/dnode/helpers"
2519 "github.com/dfinance/dnode/helpers/tests"
2620 cliTester "github.com/dfinance/dnode/helpers/tests/clitester"
21+ "github.com/dfinance/dnode/helpers/tests/mockdvm"
2722 testUtils "github.com/dfinance/dnode/helpers/tests/utils"
2823 "github.com/dfinance/dnode/x/vm"
2924 "github.com/dfinance/dnode/x/vm/client/vm_client"
3025)
3126
32- type MockDVM struct {
33- server * grpc.Server
34- failExecution bool
35- failResponse bool
36- execDelay time.Duration
37- }
38-
39- func (s * MockDVM ) SetExecutionFail () { s .failExecution = true }
40- func (s * MockDVM ) SetExecutionOK () { s .failExecution = false }
41- func (s * MockDVM ) SetResponseFail () { s .failResponse = true }
42- func (s * MockDVM ) SetResponseOK () { s .failResponse = false }
43- func (s * MockDVM ) SetExecutionDelay (dur time.Duration ) {
44- s .execDelay = dur
45- }
46- func (s * MockDVM ) Stop () {
47- if s .server != nil {
48- s .server .Stop ()
49- }
50- }
51-
52- func (s * MockDVM ) PublishModule (ctx context.Context , in * vm_grpc.VMPublishModule ) (* vm_grpc.VMExecuteResponse , error ) {
53- if s .failExecution {
54- return nil , grpcStatus .Errorf (codes .Internal , "failing gRPC execution" )
55- }
56-
57- resp := & vm_grpc.VMExecuteResponse {}
58- if ! s .failResponse {
59- resp = & vm_grpc.VMExecuteResponse {
60- WriteSet : nil ,
61- Events : nil ,
62- GasUsed : 1 ,
63- Status : vm_grpc .ContractStatus_Discard ,
64- StatusStruct : nil ,
65- }
66- }
67-
68- return resp , nil
69- }
70-
71- func StartMockDVMService (listener net.Listener ) * MockDVM {
72- s := & MockDVM {
73- execDelay : 100 * time .Millisecond ,
74- }
75-
76- server := grpc .NewServer ()
77- vm_grpc .RegisterVMModulePublisherServer (server , s )
78-
79- go func () {
80- server .Serve (listener )
81- }()
82- s .server = server
83-
84- return s
85- }
86-
8727// Test dnode crash on VM Tx failure
8828func TestInteg_ConsensusFailure (t * testing.T ) {
8929 const script = `
@@ -167,7 +107,7 @@ func TestIntegVM_ExecuteScriptViaCLI(t *testing.T) {
167107 ct := cliTester .New (
168108 t ,
169109 false ,
170- cliTester .VMCommunicationOption (50 , 1000 , 100 ),
110+ cliTester .VMCommunicationOption (5 , 1000 ),
171111 cliTester .VMCommunicationBaseAddressNetOption ("tcp://127.0.0.1" ),
172112 )
173113 defer ct .Close ()
@@ -254,7 +194,7 @@ func TestIntegVM_ExecuteScriptViaREST(t *testing.T) {
254194 ct := cliTester .New (
255195 t ,
256196 false ,
257- cliTester .VMCommunicationOption (50 , 1000 , 100 ),
197+ cliTester .VMCommunicationOption (5 , 1000 ),
258198 cliTester .VMCommunicationBaseAddressNetOption ("tcp://127.0.0.1" ),
259199 )
260200 defer ct .Close ()
@@ -358,7 +298,7 @@ func TestIntegVM_DeployModuleViaCLI(t *testing.T) {
358298 ct := cliTester .New (
359299 t ,
360300 false ,
361- cliTester .VMCommunicationOption (50 , 1000 , 100 ),
301+ cliTester .VMCommunicationOption (5 , 1000 ),
362302 cliTester .VMCommunicationBaseAddressNetOption ("tcp://127.0.0.1" ),
363303 )
364304 defer ct .Close ()
@@ -417,7 +357,7 @@ func TestIntegVM_DeployModuleViaREST(t *testing.T) {
417357 ct := cliTester .New (
418358 t ,
419359 false ,
420- cliTester .VMCommunicationOption (50 , 1000 , 100 ),
360+ cliTester .VMCommunicationOption (5 , 1000 ),
421361 cliTester .VMCommunicationBaseAddressNetOption ("tcp://127.0.0.1" ),
422362 )
423363 defer ct .Close ()
@@ -486,7 +426,7 @@ func TestIntegVM_RequestRetry(t *testing.T) {
486426 ct := cliTester .New (
487427 t ,
488428 true ,
489- cliTester .VMCommunicationOption (100 , 500 , 10 ),
429+ cliTester .VMCommunicationOption (5 , 100 ),
490430 cliTester .VMCommunicationBaseAddressUDSOption (dsSocket , mockDVMSocket ),
491431 )
492432 defer ct .Close ()
@@ -496,7 +436,7 @@ func TestIntegVM_RequestRetry(t *testing.T) {
496436 mockDVMListener , err := helpers .GetGRpcNetListener ("unix://" + mockDVMSocketPath )
497437 require .NoError (t , err , "creating MockDVM listener" )
498438
499- mockDvm := StartMockDVMService (mockDVMListener )
439+ mockDvm := mockdvm . StartMockDVMService (mockDVMListener )
500440 defer mockDvm .Stop ()
501441 require .NoError (t , testUtils .WaitForFileExists (mockDVMSocketPath , 1 * time .Second ), "MockDVM start failed" )
502442
@@ -580,7 +520,7 @@ func TestIntegVM_CommunicationUDS(t *testing.T) {
580520 ct := cliTester .New (
581521 t ,
582522 false ,
583- cliTester .VMCommunicationOption (50 , 1000 , 100 ),
523+ cliTester .VMCommunicationOption (5 , 1000 ),
584524 cliTester .VMCommunicationBaseAddressUDSOption (dsSocket , dvmSocket ),
585525 )
586526 defer ct .Close ()
0 commit comments