Files
wehub-resource-sync 498b235461
Build and test / Build and test AMD64 Ubuntu 22.04 (push) Failing after 0s
Publish Builder / amazonlinux2023 (push) Failing after 1s
Build and test / UT for Go (push) Has been skipped
Publish KRTE Images / KRTE (push) Failing after 1s
Build and test / Integration Test (push) Has been skipped
Build and test / Upload Code Coverage (push) Has been skipped
Publish Builder / rockylinux9 (push) Failing after 1s
Publish Builder / ubuntu22.04 (push) Failing after 0s
Publish Builder / ubuntu24.04 (push) Failing after 0s
Publish Gpu Builder / publish-gpu-builder (push) Failing after 1s
Publish Test Images / PyTest (push) Failing after 0s
Build and test / UT for Cpp (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:17 +08:00

66 lines
1.7 KiB
Go

package connection
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
func Test_getIdentifierFromContext(t *testing.T) {
t.Run("metadata not found", func(t *testing.T) {
ctx := context.TODO()
_, err := GetIdentifierFromContext(ctx)
assert.Error(t, err)
})
t.Run("no identifier", func(t *testing.T) {
md := metadata.New(map[string]string{})
ctx := metadata.NewIncomingContext(context.TODO(), md)
_, err := GetIdentifierFromContext(ctx)
assert.Error(t, err)
})
t.Run("invalid identifier", func(t *testing.T) {
md := metadata.New(map[string]string{
"identifier": "i-am-not-invalid-identifier",
})
ctx := metadata.NewIncomingContext(context.TODO(), md)
_, err := GetIdentifierFromContext(ctx)
assert.Error(t, err)
})
t.Run("normal case", func(t *testing.T) {
md := metadata.New(map[string]string{
"identifier": "20230518",
})
ctx := metadata.NewIncomingContext(context.TODO(), md)
identifier, err := GetIdentifierFromContext(ctx)
assert.NoError(t, err)
assert.Equal(t, int64(20230518), identifier)
})
}
func TestKeepActiveInterceptor(t *testing.T) {
md := metadata.New(map[string]string{
"identifier": "20230518",
})
ctx := metadata.NewIncomingContext(context.TODO(), md)
rpcCalled := false
rpcChan := make(chan struct{}, 1)
var handler grpc.UnaryHandler = func(ctx context.Context, req interface{}) (interface{}, error) {
rpcCalled = true
rpcChan <- struct{}{}
return "not-important", nil
}
got, err := KeepActiveInterceptor(ctx, nil, nil, handler)
<-rpcChan
assert.True(t, rpcCalled)
assert.NoError(t, err)
assert.Equal(t, "not-important", got)
}