github.com/eth-easl/loader@v0.0.0-20230908084258-8a37e1d94279/pkg/driver/grpc_client_test.go (about)

     1  /*
     2   * MIT License
     3   *
     4   * Copyright (c) 2023 EASL and the vHive community
     5   *
     6   * Permission is hereby granted, free of charge, to any person obtaining a copy
     7   * of this software and associated documentation files (the "Software"), to deal
     8   * in the Software without restriction, including without limitation the rights
     9   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10   * copies of the Software, and to permit persons to whom the Software is
    11   * furnished to do so, subject to the following conditions:
    12   *
    13   * The above copyright notice and this permission notice shall be included in all
    14   * copies or substantial portions of the Software.
    15   *
    16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22   * SOFTWARE.
    23   */
    24  
    25  package driver
    26  
    27  import (
    28  	"fmt"
    29  	"os"
    30  	"testing"
    31  	"time"
    32  
    33  	"github.com/eth-easl/loader/pkg/common"
    34  	"github.com/eth-easl/loader/pkg/config"
    35  	"github.com/eth-easl/loader/pkg/workload/standard"
    36  	"github.com/sirupsen/logrus"
    37  )
    38  
    39  func createFakeLoaderConfiguration() *config.LoaderConfiguration {
    40  	return &config.LoaderConfiguration{
    41  		Platform:                     "Knative",
    42  		OutputPathPrefix:             "test",
    43  		EnableZipkinTracing:          true,
    44  		GRPCConnectionTimeoutSeconds: 5,
    45  		GRPCFunctionTimeoutSeconds:   15,
    46  	}
    47  }
    48  
    49  var testFunction = common.Function{
    50  	Name: "test-function",
    51  }
    52  
    53  var testRuntimeSpecs = common.RuntimeSpecification{
    54  	Runtime: 10, // ms
    55  	Memory:  128,
    56  }
    57  
    58  func TestGRPCClientWithServerUnreachable(t *testing.T) {
    59  	cfg := createFakeLoaderConfiguration()
    60  	cfg.EnableZipkinTracing = true
    61  
    62  	success, record := Invoke(&testFunction, &testRuntimeSpecs, cfg)
    63  
    64  	if record.Instance != "" ||
    65  		record.RequestedDuration != uint32(testRuntimeSpecs.Runtime*1000) ||
    66  		record.StartTime == 0 ||
    67  		record.ResponseTime == 0 ||
    68  		success != false ||
    69  		record.ConnectionTimeout != true {
    70  
    71  		t.Error("Error while testing an unreachable server.")
    72  	}
    73  }
    74  
    75  func TestGRPCClientWithServerReachable(t *testing.T) {
    76  	address, port := "localhost", 8080
    77  	testFunction.Endpoint = fmt.Sprintf("%s:%d", address, port)
    78  
    79  	go standard.StartGRPCServer(address, port, standard.TraceFunction, "")
    80  
    81  	// make sure that the gRPC server is running
    82  	time.Sleep(2 * time.Second)
    83  
    84  	cfg := createFakeLoaderConfiguration()
    85  
    86  	success, record := Invoke(&testFunction, &testRuntimeSpecs, cfg)
    87  
    88  	if !success ||
    89  		record.MemoryAllocationTimeout != false ||
    90  		record.ConnectionTimeout != false ||
    91  		record.FunctionTimeout != false ||
    92  		record.ResponseTime == 0 ||
    93  		record.ActualDuration == 0 ||
    94  		record.ActualMemoryUsage == 0 {
    95  
    96  		t.Error("Failed gRPC invocations.")
    97  	}
    98  }
    99  
   100  func TestGRPCClientWithServerBatchWorkload(t *testing.T) {
   101  	logrus.SetLevel(logrus.TraceLevel)
   102  	err := os.Setenv("ITERATIONS_MULTIPLIER", "225")
   103  	if err != nil {
   104  		t.Error(err)
   105  	}
   106  
   107  	address, port := "localhost", 8081
   108  	testFunction.Endpoint = fmt.Sprintf("%s:%d", address, port)
   109  
   110  	go standard.StartGRPCServer(address, port, standard.TraceFunction, "")
   111  
   112  	// make sure that the gRPC server is running
   113  	time.Sleep(2 * time.Second)
   114  
   115  	cfg := createFakeLoaderConfiguration()
   116  
   117  	for i := 0; i < 50; i++ {
   118  		success, record := Invoke(&testFunction, &testRuntimeSpecs, cfg)
   119  
   120  		if !success ||
   121  			record.MemoryAllocationTimeout != false ||
   122  			record.ConnectionTimeout != false ||
   123  			record.FunctionTimeout != false ||
   124  			record.ResponseTime == 0 ||
   125  			record.ActualDuration == 0 ||
   126  			record.ActualMemoryUsage == 0 {
   127  
   128  			t.Error("Failed gRPC invocations.")
   129  		}
   130  	}
   131  }