forked from lavanet/lava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PRT-343 reach by default to maximum number of connections available (l…
…avanet#263) * PRT-343 adding connection resource managment on stress and relax * PRT-343 adding tests to connector and grpc connector. * PRT-343 lint and log fix * PRT-343 lint * PRT-343 more indicative prints and improving functionality of removing a free client
- Loading branch information
1 parent
560abd6
commit dc177fa
Showing
8 changed files
with
217 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package chainproxy | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/rpc" | ||
"testing" | ||
"time" | ||
|
||
"github.com/lavanet/lava/relayer/chainproxy/rpcclient" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
listenerAddress = "localhost:1234" | ||
listenerAddressTcp = "http://localhost:1234" | ||
numberOfClients = 5 | ||
) | ||
|
||
type Args struct{} | ||
|
||
type TimeServer int64 | ||
|
||
func (t *TimeServer) GiveServerTime(args *Args, reply *int64) error { | ||
// Set the value at the pointer got from the client | ||
*reply = time.Now().Unix() | ||
return nil | ||
} | ||
|
||
func createGRPCServer(t *testing.T) *grpc.Server { | ||
lis, err := net.Listen("tcp", listenerAddress) | ||
require.Nil(t, err) | ||
s := grpc.NewServer() | ||
go s.Serve(lis) // serve in a different thread | ||
return s | ||
} | ||
|
||
func createRPCServer(t *testing.T) net.Listener { | ||
timeserver := new(TimeServer) | ||
// Register the timeserver object upon which the GiveServerTime | ||
// function will be called from the RPC server (from the client) | ||
rpc.Register(timeserver) | ||
// Registers an HTTP handler for RPC messages | ||
rpc.HandleHTTP() | ||
// Start listening for the requests on port 1234 | ||
listener, err := net.Listen("tcp", listenerAddress) | ||
if err != nil { | ||
log.Fatal("Listener error: ", err) | ||
} | ||
// Serve accepts incoming HTTP connections on the listener l, creating | ||
// a new service goroutine for each. The service goroutines read requests | ||
// and then call handler to reply to them | ||
go http.Serve(listener, nil) | ||
|
||
return listener | ||
} | ||
|
||
func TestConnector(t *testing.T) { | ||
listener := createRPCServer(t) // create a grpcServer so we can connect to its endpoint and validate everything works. | ||
defer listener.Close() | ||
ctx := context.Background() | ||
conn := NewConnector(ctx, numberOfClients, listenerAddressTcp) | ||
require.Equal(t, len(conn.freeClients), numberOfClients) | ||
increasedClients := numberOfClients * 2 // increase to double the number of clients | ||
rpcList := make([]*rpcclient.Client, increasedClients) | ||
for i := 0; i < increasedClients; i++ { | ||
rpc, err := conn.GetRpc(ctx, true) | ||
require.Nil(t, err) | ||
rpcList[i] = rpc | ||
} | ||
require.Equal(t, conn.usedClients, increasedClients) // checking we have used clients | ||
for i := 0; i < increasedClients; i++ { | ||
conn.ReturnRpc(rpcList[i]) | ||
} | ||
require.Equal(t, conn.usedClients, 0) // checking we dont have clients used | ||
require.Equal(t, len(conn.freeClients), increasedClients) // checking we cleaned clients | ||
} | ||
|
||
func TestConnectorGrpc(t *testing.T) { | ||
server := createGRPCServer(t) // create a grpcServer so we can connect to its endpoint and validate everything works. | ||
defer server.Stop() | ||
ctx := context.Background() | ||
conn := NewGRPCConnector(ctx, numberOfClients, listenerAddress) | ||
require.Equal(t, len(conn.freeClients), numberOfClients) | ||
increasedClients := numberOfClients * 2 // increase to double the number of clients | ||
rpcList := make([]*grpc.ClientConn, increasedClients) | ||
for i := 0; i < increasedClients; i++ { | ||
rpc, err := conn.GetRpc(ctx, true) | ||
require.Nil(t, err) | ||
rpcList[i] = rpc | ||
} | ||
require.Equal(t, conn.usedClients, increasedClients) // checking we have used clients | ||
for i := 0; i < increasedClients; i++ { | ||
conn.ReturnRpc(rpcList[i]) | ||
} | ||
require.Equal(t, conn.usedClients, 0) // checking we dont have clients used | ||
require.Equal(t, len(conn.freeClients), increasedClients) // checking we cleaned clients | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.