|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package rpc |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 13 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/util/stop" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "storj.io/drpc" |
| 17 | +) |
| 18 | + |
| 19 | +// dummyStream is a minimal implementation of drpc.Stream used for testing the |
| 20 | +// stream interceptor. |
| 21 | +type dummyStream struct { |
| 22 | + ctx context.Context |
| 23 | +} |
| 24 | + |
| 25 | +func (d dummyStream) Context() context.Context { return d.ctx } |
| 26 | +func (d dummyStream) MsgSend(drpc.Message, drpc.Encoding) error { return nil } |
| 27 | +func (d dummyStream) MsgRecv(drpc.Message, drpc.Encoding) error { return nil } |
| 28 | +func (d dummyStream) CloseSend() error { return nil } |
| 29 | +func (d dummyStream) Close() error { return nil } |
| 30 | + |
| 31 | +// TestMakeStopperInterceptors verifies that the stopper interceptors allow RPCs |
| 32 | +// to run before the stopper quiesces and reject them afterward. |
| 33 | +func TestMakeStopperInterceptors(t *testing.T) { |
| 34 | + defer leaktest.AfterTest(t)() |
| 35 | + defer log.Scope(t).Close(t) |
| 36 | + ctx := context.Background() |
| 37 | + stopper := stop.NewStopper() |
| 38 | + defer stopper.Stop(ctx) |
| 39 | + |
| 40 | + rpcCtx := &Context{ContextOptions: ContextOptions{Stopper: stopper}} |
| 41 | + |
| 42 | + unaryInterceptor, streamInterceptor := makeStopperInterceptors(rpcCtx) |
| 43 | + |
| 44 | + // Before quiesce runs. |
| 45 | + called := false |
| 46 | + _, err := unaryInterceptor(ctx, nil, "test", func(ctx context.Context, req interface{}) (interface{}, error) { |
| 47 | + called = true |
| 48 | + return nil, nil |
| 49 | + }) |
| 50 | + require.NoError(t, err) |
| 51 | + require.True(t, called) |
| 52 | + |
| 53 | + called = false |
| 54 | + _, err = streamInterceptor(dummyStream{ctx: ctx}, "test", func(stream drpc.Stream) (interface{}, error) { |
| 55 | + called = true |
| 56 | + return nil, nil |
| 57 | + }) |
| 58 | + require.NoError(t, err) |
| 59 | + require.True(t, called) |
| 60 | + |
| 61 | + // After quiesce, RPCs are rejected. |
| 62 | + stopper.Quiesce(ctx) |
| 63 | + |
| 64 | + called = false |
| 65 | + _, err = unaryInterceptor(ctx, nil, "test", func(ctx context.Context, req interface{}) (interface{}, error) { |
| 66 | + called = true |
| 67 | + return nil, nil |
| 68 | + }) |
| 69 | + require.ErrorIs(t, err, stop.ErrUnavailable) |
| 70 | + require.False(t, called) |
| 71 | + |
| 72 | + called = false |
| 73 | + _, err = streamInterceptor(dummyStream{ctx: ctx}, "test", func(stream drpc.Stream) (interface{}, error) { |
| 74 | + called = true |
| 75 | + return nil, nil |
| 76 | + }) |
| 77 | + require.ErrorIs(t, err, stop.ErrUnavailable) |
| 78 | + require.False(t, called) |
| 79 | +} |
0 commit comments