Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
MakeHoney committed Sep 17, 2018
1 parent ba46d5e commit 308e17d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 8 deletions.
107 changes: 107 additions & 0 deletions api_gateway/connection_query_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package api_gateway_test

import (
"testing"

"github.com/it-chain/engine/api_gateway"
"github.com/it-chain/engine/common/event"
"github.com/stretchr/testify/assert"
)

func TestConnectionQueryApi_SaveConnection(t *testing.T) {
repo := api_gateway.NewConnectionRepository()
api := api_gateway.NewConnectionQueryApi(*repo)
err := api.Save(api_gateway.Connection{
ConnectionID: "1",
Address: "address",
})
assert.NoError(t, err)

tests := map[string]struct {
Input api_gateway.Connection
Output error
}{
"success": {
Input: api_gateway.Connection{
ConnectionID: "0",
Address: "address",
},
Output: nil,
},
"fail": {
Input: api_gateway.Connection{
ConnectionID: "1",
Address: "address",
},
Output: api_gateway.ErrConnectionExists,
},
}

for testName, test := range tests {
t.Logf("Running '%s' test, caseName: %s", t.Name(), testName)
//given

err := api.Save(test.Input)
if err != nil {
assert.Equal(t, test.Output, err)
continue
}

c, exist := repo.ConnectionTable[test.Input.ConnectionID]
assert.True(t, exist)
assert.Equal(t, test.Output, err)
assert.Equal(t, test.Input.ConnectionID, c.ConnectionID)
assert.Equal(t, test.Input.ConnectionID, c.ConnectionID)
}

}

func TestConnectionQueryApi_RemoveConnection(t *testing.T) {
repo := api_gateway.NewConnectionRepository()
api := api_gateway.NewConnectionQueryApi(*repo)
err := api.Save(api_gateway.Connection{
ConnectionID: "0",
Address: "address",
})
assert.NoError(t, err)

c, exist := repo.ConnectionTable["0"]
assert.True(t, exist)

api.Remove(c.ConnectionID)
_, exist = repo.ConnectionTable["0"]
assert.False(t, exist)
}

func TestConnectionEventListener_HandleConnectionCreatedEvent(t *testing.T) {
repo := api_gateway.NewConnectionRepository()
api := api_gateway.NewConnectionQueryApi(*repo)
listener := api_gateway.NewConnectionEventListener(*api)

err := listener.HandleConnectionCreatedEvent(event.ConnectionCreated{
ConnectionID: "0",
Address: "address",
})
assert.NoError(t, err)
}

func TestConnectionEventListener_HandleConnectionClosedEvent(t *testing.T) {
repo := api_gateway.NewConnectionRepository()
api := api_gateway.NewConnectionQueryApi(*repo)
listener := api_gateway.NewConnectionEventListener(*api)

err := listener.HandleConnectionCreatedEvent(event.ConnectionCreated{
ConnectionID: "0",
Address: "address",
})
assert.NoError(t, err)

c, exist := repo.ConnectionTable["0"]
assert.True(t, exist)

listener.HandleConnectionClosedEvent(event.ConnectionClosed{
ConnectionID: c.ConnectionID,
})
_, exist = repo.ConnectionTable["0"]
assert.False(t, exist)
}
14 changes: 7 additions & 7 deletions grpc_gateway/infra/grpc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func TestGrpcHostService_SendMessages(t *testing.T) {

handler := &MockHandler{}
handler.OnConnectionFunc = func(connection grpc_gateway.Connection) {
connID = connection.ConnectionId
connID = connection.ConnectionID
}

handler.OnDisconnectionFunc = func(connection grpc_gateway.Connection) {
Expand All @@ -409,7 +409,7 @@ func TestGrpcHostService_SendMessages(t *testing.T) {

publishedData = test.input.Message

clientHostService.SendMessages(test.input.Message, test.input.Protocol, conn.ConnectionId)
clientHostService.SendMessages(test.input.Message, test.input.Protocol, conn.ConnectionID)
}
}

Expand Down Expand Up @@ -452,12 +452,12 @@ func TestGrpcHostService_Close(t *testing.T) {
handler := &MockHandler{}
handler.OnConnectionFunc = func(connection grpc_gateway.Connection) {
fmt.Println(connection)
connID = connection.ConnectionId
connID = connection.ConnectionID
}

handler.OnDisconnectionFunc = func(connection grpc_gateway.Connection) {
fmt.Println("connection is closing", connection)
assert.Equal(t, connID, connection.ConnectionId)
assert.Equal(t, connID, connection.ConnectionID)
}

serverHostService.SetHandler(handler)
Expand All @@ -468,7 +468,7 @@ func TestGrpcHostService_Close(t *testing.T) {

conn, err := clientHostService.Dial(test.input)
assert.NoError(t, err)
clientHostService.CloseConnection(conn.ConnectionId)
clientHostService.CloseConnection(conn.ConnectionID)
}
}

Expand Down Expand Up @@ -503,7 +503,7 @@ func TestGrpcHostService_GetAllConnections(t *testing.T) {
var connID string
handler := &MockHandler{}
handler.OnConnectionFunc = func(connection grpc_gateway.Connection) {
connID = connection.ConnectionId
connID = connection.ConnectionID
}

handler.OnDisconnectionFunc = func(connection grpc_gateway.Connection) {
Expand All @@ -518,5 +518,5 @@ func TestGrpcHostService_GetAllConnections(t *testing.T) {

connections, err := serverHostService.GetAllConnections()
assert.NoError(t, err)
assert.Equal(t, connections[0].ConnectionId, connID)
assert.Equal(t, connections[0].ConnectionID, connID)
}
2 changes: 1 addition & 1 deletion p2p/infra/adapter/event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestEventHandler_HandleConnDisconnectedEvent(t *testing.T) {
t.Logf("running test case %s", testName)

e := event.ConnectionClosed{
ConnectionId: test.input.id,
ConnectionID: test.input.id,
}

err := eventHandler.HandleConnDisconnectedEvent(e)
Expand Down

0 comments on commit 308e17d

Please sign in to comment.