mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 10:26:29 +02:00
* wip * wip * handle wildcards in override name * remove wait for ready, add comment about sync, force initial sync complete in test * address comments
36 lines
1,013 B
Go
36 lines
1,013 B
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
func Test_grpcTimeoutInterceptor(t *testing.T) {
|
|
mockInvoker := func(sleepTime time.Duration, wantFail bool) grpc.UnaryInvoker {
|
|
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
|
|
time.Sleep(sleepTime)
|
|
deadline, ok := ctx.Deadline()
|
|
if !ok {
|
|
t.Fatal("No deadline set")
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
if ok && now.After(deadline) && !wantFail {
|
|
t.Errorf("Deadline exceeded, but should not have. now=%v, deadline=%v", now, deadline)
|
|
} else if now.Before(deadline) && wantFail {
|
|
t.Errorf("Deadline not exceeded, but should have. now=%v, deadline=%v", now, deadline)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
timeOut := 300 * time.Millisecond
|
|
to := grpcTimeoutInterceptor(timeOut)
|
|
|
|
to(context.Background(), "test", nil, nil, nil, mockInvoker(timeOut*2, true))
|
|
to(context.Background(), "test", nil, nil, nil, mockInvoker(timeOut/2, false))
|
|
}
|