TestDataCachePurging verifies that the LB policy periodically evicts expired entries from the data cache.
(t *testing.T)
| 897 | // TestDataCachePurging verifies that the LB policy periodically evicts expired |
| 898 | // entries from the data cache. |
| 899 | func (s) TestDataCachePurging(t *testing.T) { |
| 900 | // Override the frequency of the data cache purger to a small one. |
| 901 | origDataCachePurgeTicker := dataCachePurgeTicker |
| 902 | ticker := time.NewTicker(defaultTestShortTimeout) |
| 903 | defer ticker.Stop() |
| 904 | dataCachePurgeTicker = func() *time.Ticker { return ticker } |
| 905 | defer func() { dataCachePurgeTicker = origDataCachePurgeTicker }() |
| 906 | |
| 907 | // Override the data cache purge hook to get notified. |
| 908 | dataCachePurgeDone := make(chan struct{}, 1) |
| 909 | origDataCachePurgeHook := dataCachePurgeHook |
| 910 | dataCachePurgeHook = func() { dataCachePurgeDone <- struct{}{} } |
| 911 | defer func() { dataCachePurgeHook = origDataCachePurgeHook }() |
| 912 | |
| 913 | // Start an RLS server and set the throttler to never throttle requests. |
| 914 | rlsServer, rlsReqCh := rlstest.SetupFakeRLSServer(t, nil) |
| 915 | overrideAdaptiveThrottler(t, neverThrottlingThrottler()) |
| 916 | |
| 917 | // Register an LB policy to act as the child policy for RLS LB policy. |
| 918 | childPolicyName := "test-child-policy" + t.Name() |
| 919 | e2e.RegisterRLSChildPolicy(childPolicyName, nil) |
| 920 | t.Logf("Registered child policy with name %q", childPolicyName) |
| 921 | |
| 922 | // Build RLS service config with header matchers and lookupService pointing to |
| 923 | // the fake RLS server created above. Set a very low value for maxAge to |
| 924 | // ensure that the entry expires soon. |
| 925 | rlsConfig := buildBasicRLSConfig(childPolicyName, rlsServer.Address) |
| 926 | rlsConfig.RouteLookupConfig.MaxAge = durationpb.New(time.Millisecond) |
| 927 | |
| 928 | // Start a test backend, and set up the fake RLS server to return this as a |
| 929 | // target in the RLS response. |
| 930 | backendCh, backendAddress := startBackend(t) |
| 931 | rlsServer.SetResponseCallback(func(_ context.Context, _ *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse { |
| 932 | return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{Targets: []string{backendAddress}}} |
| 933 | }) |
| 934 | |
| 935 | // Register a manual resolver and push the RLS service config through it. |
| 936 | r := startManualResolverWithConfig(t, rlsConfig) |
| 937 | |
| 938 | cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 939 | if err != nil { |
| 940 | t.Fatalf("Failed to create gRPC client: %v", err) |
| 941 | } |
| 942 | defer cc.Close() |
| 943 | |
| 944 | // Make an RPC and ensure it gets routed to the test backend. |
| 945 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 946 | defer cancel() |
| 947 | ctxOutgoing := metadata.AppendToOutgoingContext(ctx, "n1", "v1") |
| 948 | makeTestRPCAndExpectItToReachBackend(ctxOutgoing, t, cc, backendCh) |
| 949 | |
| 950 | // Make sure an RLS request is sent out. |
| 951 | verifyRLSRequest(t, rlsReqCh, true) |
| 952 | |
| 953 | // Make another RPC with different headers. This will force the LB policy to |
| 954 | // send out a new RLS request, resulting in a new data cache entry. |
| 955 | ctxOutgoing = metadata.AppendToOutgoingContext(ctx, "n2", "v2") |
| 956 | makeTestRPCAndExpectItToReachBackend(ctxOutgoing, t, cc, backendCh) |
nothing calls this directly
no test coverage detected