github.com/grafana/synthetic-monitoring-agent@v0.64.2/cmd/test-api/internal/grpc/grpc_test.go (about)

     1  package grpc
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
     9  	"github.com/rs/zerolog"
    10  	"github.com/stretchr/testify/require"
    11  	"google.golang.org/grpc"
    12  )
    13  
    14  func TestNewServer(t *testing.T) {
    15  	testCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    16  	t.Cleanup(cancel)
    17  
    18  	if deadline, ok := t.Deadline(); ok {
    19  		ctx, cancel := context.WithDeadline(context.Background(), deadline)
    20  		t.Cleanup(cancel)
    21  
    22  		testCtx = ctx
    23  	}
    24  
    25  	s, err := NewServer(
    26  		testCtx,
    27  		&Opts{
    28  			Logger:            zerolog.New(zerolog.NewTestWriter(t)),
    29  			ListenAddr:        "0.0.0.0:",
    30  			ChecksServer:      &testChecksServer{},
    31  			TenantsServer:     &testTenantsServer{},
    32  			AdHocChecksServer: &testAdHocChecksServer{},
    33  			Db:                &testDb{},
    34  		},
    35  	)
    36  
    37  	require.NoError(t, err)
    38  	require.NotNil(t, s)
    39  
    40  	res := make(chan error)
    41  
    42  	go func() {
    43  		res <- s.Run(testCtx)
    44  	}()
    45  
    46  	time.Sleep(100 * time.Millisecond) // give the server some time to start
    47  	s.Stop()
    48  
    49  	select {
    50  	case <-testCtx.Done():
    51  		require.False(t, true, "deadline exceeded")
    52  	case err := <-res:
    53  		if err != nil {
    54  			require.ErrorIs(t, err, grpc.ErrServerStopped)
    55  		}
    56  	}
    57  }
    58  
    59  type testChecksServer struct{}
    60  
    61  func (s *testChecksServer) RegisterProbe(context.Context, *sm.ProbeInfo) (*sm.RegisterProbeResult, error) {
    62  	return nil, nil
    63  }
    64  
    65  func (s *testChecksServer) GetChanges(*sm.ProbeState, sm.Checks_GetChangesServer) error {
    66  	return nil
    67  }
    68  
    69  func (s *testChecksServer) Ping(context.Context, *sm.PingRequest) (*sm.PongResponse, error) {
    70  	return nil, nil
    71  }
    72  
    73  type testTenantsServer struct{}
    74  
    75  func (s *testTenantsServer) GetTenant(context.Context, *sm.TenantInfo) (*sm.Tenant, error) {
    76  	return nil, nil
    77  }
    78  
    79  type testAdHocChecksServer struct{}
    80  
    81  func (s *testAdHocChecksServer) RegisterProbe(context.Context, *sm.ProbeInfo) (*sm.RegisterProbeResult, error) {
    82  	return nil, nil
    83  }
    84  
    85  func (s *testAdHocChecksServer) GetAdHocChecks(*sm.Void, sm.AdHocChecks_GetAdHocChecksServer) error {
    86  	return nil
    87  }
    88  
    89  type testDb struct{}
    90  
    91  func (db *testDb) FindProbeIDByToken(ctx context.Context, token []byte) (int64, error) {
    92  	return 0, nil
    93  }