go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/rpcutil/rpcutil_test.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rpcutil 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "google.golang.org/grpc" 23 "google.golang.org/grpc/codes" 24 "google.golang.org/grpc/status" 25 26 "go.chromium.org/luci/common/tsmon" 27 tsmonstore "go.chromium.org/luci/common/tsmon/store" 28 "go.chromium.org/luci/common/tsmon/target" 29 "go.chromium.org/luci/server/auth" 30 "go.chromium.org/luci/server/auth/authtest" 31 32 . "github.com/smartystreets/goconvey/convey" 33 ) 34 35 func TestIdentityKindCountingInterceptor(t *testing.T) { 36 ctx, _ := tsmon.WithDummyInMemory(context.Background()) 37 store := tsmon.Store(ctx) 38 interceptor := IdentityKindCountingInterceptor() 39 fakeServerInfo := grpc.UnaryServerInfo{FullMethod: "/service/method"} 40 fakeHandler := func(_ context.Context, _ any) (any, error) { 41 return struct{}{}, status.Error(codes.Internal, "dummy error") 42 } 43 44 Convey(`Increments counter`, t, func() { 45 Reset(func() { 46 tsmon.GetState(ctx).SetStore(tsmonstore.NewInMemory(&target.Task{})) 47 store = tsmon.GetState(ctx).Store() 48 }) 49 Convey(`user`, func() { 50 ctx := auth.WithState(ctx, &authtest.FakeState{ 51 Identity: "user:user@example.com", 52 }) 53 _, _ = interceptor(ctx, nil, &fakeServerInfo, fakeHandler) 54 So(store.Get(ctx, IdentityKindCounter, time.Time{}, []any{"service", "method", "user"}), ShouldEqual, 1) 55 }) 56 57 Convey(`anonymous`, func() { 58 _, _ = interceptor(ctx, nil, &fakeServerInfo, fakeHandler) 59 So(store.Get(ctx, IdentityKindCounter, time.Time{}, []any{"service", "method", "user"}), ShouldBeNil) 60 So(store.Get(ctx, IdentityKindCounter, time.Time{}, []any{"service", "method", "anonymous"}), ShouldEqual, 1) 61 }) 62 }) 63 }