File: timeout_test.go

package info (click to toggle)
golang-github-grpc-ecosystem-go-grpc-middleware 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,564 kB
  • sloc: makefile: 100; sh: 9
file content (55 lines) | stat: -rw-r--r-- 1,652 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.

package timeout_test

import (
	"context"
	"testing"
	"time"

	"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/timeout"
	"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
	"github.com/stretchr/testify/assert"
	"google.golang.org/grpc"
)

type TimeoutTestServiceServer struct {
	sleepTime time.Duration
	testpb.TestPingService
}

func (t *TimeoutTestServiceServer) Ping(ctx context.Context, req *testpb.PingRequest) (*testpb.PingResponse, error) {
	if t.sleepTime > 0 {
		time.Sleep(t.sleepTime)
	}
	return t.TestPingService.Ping(ctx, req)
}

func TestTimeoutUnaryClientInterceptor(t *testing.T) {
	server := &TimeoutTestServiceServer{}

	its := &testpb.InterceptorTestSuite{
		ClientOpts: []grpc.DialOption{
			grpc.WithUnaryInterceptor(timeout.UnaryClientInterceptor(100 * time.Millisecond)),
		},
		TestService: server,
	}
	its.Suite.SetT(t)
	its.SetupSuite()
	defer its.TearDownSuite()

	// This call will take 0/100ms for respond, so the client timeout NOT exceed.
	resp, err := its.Client.Ping(context.TODO(), &testpb.PingRequest{Value: "default_response_value"})
	assert.NoError(t, err)
	assert.NotNil(t, resp)
	assert.Equal(t, "default_response_value", resp.Value)

	// server will sleep 300ms before respond
	server.sleepTime = 300 * time.Millisecond

	// This call will take 300/100ms for respond, so the client timeout exceed.
	resp2, err2 := its.Client.Ping(context.TODO(), &testpb.PingRequest{})
	assert.Nil(t, resp2)
	assert.EqualError(t, err2, "rpc error: code = DeadlineExceeded desc = context deadline exceeded")
}