2525package clients
2626
2727import (
28+ "context"
2829 "fmt"
30+ "github.com/sirupsen/logrus"
31+ "github.com/vhive-serverless/loader/pkg/common"
2932 "github.com/vhive-serverless/loader/pkg/config"
33+ "github.com/vhive-serverless/loader/pkg/workload/standard"
34+ helloworld "github.com/vhive-serverless/vSwarm/utils/protobuf/helloworld"
35+ "google.golang.org/grpc"
36+ "google.golang.org/grpc/reflection"
37+ "net"
3038 "os"
3139 "testing"
3240 "time"
33-
34- "github.com/sirupsen/logrus"
35- "github.com/vhive-serverless/loader/pkg/common"
36- "github.com/vhive-serverless/loader/pkg/workload/standard"
3741)
3842
3943func createFakeLoaderConfiguration () * config.LoaderConfiguration {
@@ -46,6 +50,17 @@ func createFakeLoaderConfiguration() *config.LoaderConfiguration {
4650 GRPCFunctionTimeoutSeconds : 15 ,
4751 }
4852}
53+ func createFakeVSwarmLoaderConfiguration () * config.LoaderConfiguration {
54+ return & config.LoaderConfiguration {
55+ Platform : "Knative" ,
56+ InvokeProtocol : "grpc" ,
57+ OutputPathPrefix : "test" ,
58+ EnableZipkinTracing : true ,
59+ GRPCConnectionTimeoutSeconds : 5 ,
60+ GRPCFunctionTimeoutSeconds : 15 ,
61+ VSwarm : true ,
62+ }
63+ }
4964
5065var testFunction = common.Function {
5166 Name : "test-function" ,
@@ -56,6 +71,29 @@ var testRuntimeSpecs = common.RuntimeSpecification{
5671 Memory : 128 ,
5772}
5873
74+ type vSwarmServer struct {
75+ helloworld.UnimplementedGreeterServer
76+ }
77+
78+ func (s * vSwarmServer ) SayHello (_ context.Context , req * helloworld.HelloRequest ) (* helloworld.HelloReply , error ) {
79+ return & helloworld.HelloReply {
80+ Message : "Reply message" ,
81+ }, nil
82+ }
83+
84+ func startVSwarmGRPCServer (serverAddress string , serverPort int ) {
85+ lis , err := net .Listen ("tcp" , fmt .Sprintf ("%s:%d" , serverAddress , serverPort ))
86+ if err != nil {
87+ logrus .Fatalf ("failed to listen: %v" , err )
88+ }
89+
90+ grpcServer := grpc .NewServer ()
91+
92+ reflection .Register (grpcServer ) // gRPC Server Reflection is used by gRPC CLI
93+ helloworld .RegisterGreeterServer (grpcServer , & vSwarmServer {})
94+ _ = grpcServer .Serve (lis )
95+ }
96+
5997func TestGRPCClientWithServerUnreachable (t * testing.T ) {
6098 cfg := createFakeLoaderConfiguration ()
6199 cfg .EnableZipkinTracing = true
@@ -70,7 +108,24 @@ func TestGRPCClientWithServerUnreachable(t *testing.T) {
70108 success != false ||
71109 record .ConnectionTimeout != true {
72110
73- t .Error ("Error while testing an unreachable server." )
111+ t .Error ("Error while testing an unreachable server for trace function." )
112+ }
113+ }
114+
115+ func TestVSwarmClientUnreachable (t * testing.T ) {
116+ cfgSwarm := createFakeVSwarmLoaderConfiguration ()
117+
118+ vSwarmInvoker := CreateInvoker (cfgSwarm , nil , nil )
119+ success , record := vSwarmInvoker .Invoke (& testFunction , & testRuntimeSpecs )
120+
121+ if record .Instance != "" ||
122+ record .RequestedDuration != uint32 (testRuntimeSpecs .Runtime * 1000 ) ||
123+ record .StartTime == 0 ||
124+ record .ResponseTime == 0 ||
125+ success != false ||
126+ record .ConnectionTimeout != true {
127+
128+ t .Error ("Error while testing an unreachable server for vSwarm function." )
74129 }
75130}
76131
@@ -98,7 +153,32 @@ func TestGRPCClientWithServerReachable(t *testing.T) {
98153 record .ActualDuration == 0 ||
99154 record .ActualMemoryUsage == 0 {
100155
101- t .Error ("Failed gRPC invocations." )
156+ t .Error ("Failed gRPC invocations for trace function." )
157+ }
158+ }
159+
160+ func TestVSwarmClientWithServerReachable (t * testing.T ) {
161+ address , port := "localhost" , 18081
162+ testFunction .Endpoint = fmt .Sprintf ("%s:%d" , address , port )
163+
164+ go startVSwarmGRPCServer (address , port )
165+ time .Sleep (2 * time .Second )
166+
167+ cfgSwarm := createFakeVSwarmLoaderConfiguration ()
168+ vSwarmInvoker := CreateInvoker (cfgSwarm , nil , nil )
169+
170+ start := time .Now ()
171+ success , record := vSwarmInvoker .Invoke (& testFunction , & testRuntimeSpecs )
172+ logrus .Info ("Elapsed: " , time .Since (start ).Milliseconds (), " ms" )
173+
174+ if ! success ||
175+ record .MemoryAllocationTimeout != false ||
176+ record .ConnectionTimeout != false ||
177+ record .FunctionTimeout != false ||
178+ record .ResponseTime == 0 ||
179+ record .ActualDuration == 0 {
180+
181+ t .Error ("Failed gRPC invocations for vSwarm function." )
102182 }
103183}
104184
@@ -109,7 +189,7 @@ func TestGRPCClientWithServerBatchWorkload(t *testing.T) {
109189 t .Error (err )
110190 }
111191
112- address , port := "localhost" , 18081
192+ address , port := "localhost" , 18082
113193 testFunction .Endpoint = fmt .Sprintf ("%s:%d" , address , port )
114194
115195 go standard .StartGRPCServer (address , port , standard .TraceFunction , "" )
@@ -118,6 +198,7 @@ func TestGRPCClientWithServerBatchWorkload(t *testing.T) {
118198 time .Sleep (2 * time .Second )
119199
120200 cfg := createFakeLoaderConfiguration ()
201+
121202 invoker := CreateInvoker (cfg , nil , nil )
122203
123204 for i := 0 ; i < 50 ; i ++ {
@@ -131,7 +212,7 @@ func TestGRPCClientWithServerBatchWorkload(t *testing.T) {
131212 record .ActualDuration == 0 ||
132213 record .ActualMemoryUsage == 0 {
133214
134- t .Error ("Failed gRPC invocations." )
215+ t .Error ("Failed gRPC invocations for trace function ." )
135216 }
136217 }
137218}
0 commit comments