|
4 | 4 | "context" |
5 | 5 | "encoding/json" |
6 | 6 | "testing" |
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/magma-Devs/smart-router/protocol/common" |
9 | 10 | pairingtypes "github.com/magma-Devs/smart-router/types/relay" |
@@ -130,3 +131,52 @@ func TestDirectWS_PendingCheckJoinsASubscriptionStoredSinceTheActiveCheck(t *tes |
130 | 131 | "the creator's entry must not be overwritten") |
131 | 132 | require.Len(t, activeSub.connectedClients, 2) |
132 | 133 | } |
| 134 | + |
| 135 | +// TestDirectWS_CleanupOfAClientlessSubscriptionDecrementsTotalAndTellsTheNode pins the |
| 136 | +// cleanup ticker's other branch, the one the ticket's comment and MAG-3527 named: a |
| 137 | +// subscription still registered with no clients left (an Unsubscribe whose id-mapper view |
| 138 | +// said "not last" leaves one) is dropped, the counter comes back down, the node is told to |
| 139 | +// stop publishing, and the pooled connection stops counting it. Unlike the cancelled-context |
| 140 | +// branch, which a live listener reaps before the ticker can, this one is reachable. |
| 141 | +func TestDirectWS_CleanupOfAClientlessSubscriptionDecrementsTotalAndTellsTheNode(t *testing.T) { |
| 142 | + h := newReleaseHarness(t) |
| 143 | + sub := h.subscribe("ws-1") |
| 144 | + t.Cleanup(sub.disconnect) |
| 145 | + h.manager.lock.Lock() |
| 146 | + require.Len(t, h.manager.activeSubscriptions, 1) |
| 147 | + var hashedParams string |
| 148 | + var activeSub *directActiveSubscription |
| 149 | + for hp, s := range h.manager.activeSubscriptions { |
| 150 | + hashedParams, activeSub = hp, s |
| 151 | + } |
| 152 | + upstreamID := activeSub.upstreamID |
| 153 | + activeSub.releaseClientLocked(sub.clientKey) |
| 154 | + h.manager.forgetSubscriptionForClientLocked(sub.clientKey, hashedParams, false) |
| 155 | + h.manager.lock.Unlock() |
| 156 | + nodeHas := func() bool { |
| 157 | + h.srv.lock.RLock() |
| 158 | + defer h.srv.lock.RUnlock() |
| 159 | + _, ok := h.srv.subscriptions[upstreamID] |
| 160 | + return ok |
| 161 | + } |
| 162 | + pooled := func() int32 { |
| 163 | + h.manager.lock.RLock() |
| 164 | + defer h.manager.lock.RUnlock() |
| 165 | + var n int32 |
| 166 | + for _, pool := range h.manager.upstreamPools { |
| 167 | + n += pool.TotalSubscriptions() |
| 168 | + } |
| 169 | + return n |
| 170 | + } |
| 171 | + require.True(t, nodeHas()) |
| 172 | + require.Equal(t, int64(1), h.manager.totalSubscriptions.Load()) |
| 173 | + |
| 174 | + h.manager.performCleanup() |
| 175 | + |
| 176 | + require.Equal(t, int64(0), h.manager.totalSubscriptions.Load(), |
| 177 | + "cleanup deleted the subscription but left the counter high") |
| 178 | + require.Equal(t, 0, h.activeSubscriptionCount()) |
| 179 | + require.Eventually(t, func() bool { return h.upstreamCalls("eth_unsubscribe") == 1 && !nodeHas() }, |
| 180 | + 5*time.Second, 10*time.Millisecond, "the node was never told to stop publishing") |
| 181 | + require.Eventually(t, func() bool { return pooled() == 0 }, 5*time.Second, 10*time.Millisecond) |
| 182 | +} |
0 commit comments