|
| 1 | +// Copyright 2025 Democratized Data Foundation |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package p2p |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "sync" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/fxamacker/cbor/v2" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + |
| 22 | + "github.com/sourcenetwork/defradb/internal/db/p2p/protocol" |
| 23 | +) |
| 24 | + |
| 25 | +// newClosableP2P builds the smallest P2P that Close can run against: the queue and its stop |
| 26 | +// signal, plus the two subsystems Close shuts down after the workers. |
| 27 | +func newClosableP2P(queueSize int) *P2P { |
| 28 | + return &P2P{ |
| 29 | + ctx: context.Background(), |
| 30 | + host: &SimpleMockHost{}, |
| 31 | + msgQueue: make(chan queuedMessage, queueSize), |
| 32 | + msgQueueMaxBytes: 1 << 20, |
| 33 | + stopAccepting: make(chan struct{}), |
| 34 | + stopStats: make(chan struct{}), |
| 35 | + batcher: newPubsubBatcher("peerID", func(string, []byte) error { return nil }), |
| 36 | + processQueue: newProcessQueue(), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// The pubsub dispatcher keeps delivering until libp2p tears the subscription down, which Close |
| 41 | +// does not do, so the handler runs after Close has begun. A send on a closed channel is a ready |
| 42 | +// case in a select rather than a fallthrough to the default arm, so closing the queue to stop |
| 43 | +// the workers panics whichever goroutine is mid-enqueue. |
| 44 | +func TestEnqueueDuringCloseDoesNotPanic(t *testing.T) { |
| 45 | + p := newClosableP2P(4) |
| 46 | + |
| 47 | + msg, err := cbor.Marshal(protocol.PushLogRequest{DocID: "docID"}) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + // Drains so the queue keeps accepting and the handler stays on the enqueue arm, which is |
| 51 | + // the arm that panics. Stopped by its own signal rather than by closing the queue, so the |
| 52 | + // only thing that can close it is the code under test. |
| 53 | + drained := make(chan struct{}) |
| 54 | + stopDrain := make(chan struct{}) |
| 55 | + go func() { |
| 56 | + defer close(drained) |
| 57 | + for { |
| 58 | + select { |
| 59 | + case <-stopDrain: |
| 60 | + return |
| 61 | + case <-p.msgQueue: |
| 62 | + } |
| 63 | + } |
| 64 | + }() |
| 65 | + |
| 66 | + var handlers sync.WaitGroup |
| 67 | + panicked := make(chan any, 1) |
| 68 | + stop := make(chan struct{}) |
| 69 | + for range 4 { |
| 70 | + handlers.Add(1) |
| 71 | + go func() { |
| 72 | + defer handlers.Done() |
| 73 | + defer func() { |
| 74 | + if r := recover(); r != nil { |
| 75 | + select { |
| 76 | + case panicked <- r: |
| 77 | + default: |
| 78 | + } |
| 79 | + } |
| 80 | + }() |
| 81 | + for { |
| 82 | + select { |
| 83 | + case <-stop: |
| 84 | + return |
| 85 | + default: |
| 86 | + } |
| 87 | + _, _ = p.pubSubMessageHandler("sender", "topic", msg) |
| 88 | + } |
| 89 | + }() |
| 90 | + } |
| 91 | + |
| 92 | + time.Sleep(20 * time.Millisecond) |
| 93 | + p.Close() |
| 94 | + close(stop) |
| 95 | + handlers.Wait() |
| 96 | + close(stopDrain) |
| 97 | + <-drained |
| 98 | + |
| 99 | + select { |
| 100 | + case r := <-panicked: |
| 101 | + t.Fatalf("a dispatcher enqueueing during Close panicked: %v", r) |
| 102 | + default: |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Nothing closes the queue any more, so the stop signal is the only thing that ends a worker. |
| 107 | +func TestWorkersExitOnStopAccepting(t *testing.T) { |
| 108 | + p := newClosableP2P(1) |
| 109 | + |
| 110 | + done := make(chan struct{}) |
| 111 | + p.msgWorkers.Add(1) |
| 112 | + go func() { |
| 113 | + defer close(done) |
| 114 | + p.processMessageWorker() |
| 115 | + }() |
| 116 | + |
| 117 | + close(p.stopAccepting) |
| 118 | + select { |
| 119 | + case <-done: |
| 120 | + case <-time.After(5 * time.Second): |
| 121 | + t.Fatal("worker did not exit on stopAccepting") |
| 122 | + } |
| 123 | +} |
0 commit comments