|
4 | 4 | "bytes" |
5 | 5 | "context" |
6 | 6 | "errors" |
| 7 | + "fmt" |
7 | 8 | "math/big" |
8 | 9 | "testing" |
9 | 10 |
|
@@ -1048,6 +1049,11 @@ type mockUExecutorQueryClient struct { |
1048 | 1049 |
|
1049 | 1050 | // lastPendingReq records the request so tests can assert the limit sent. |
1050 | 1051 | lastPendingReq *uexecutortypes.QueryAllPendingOutboundsRequest |
| 1052 | + |
| 1053 | + // pendingReqs records every page request of a walk. |
| 1054 | + pendingReqs []*uexecutortypes.QueryAllPendingOutboundsRequest |
| 1055 | + // pendingTotal, when set, makes the mock serve that many rows by offset. |
| 1056 | + pendingTotal int |
1051 | 1057 | } |
1052 | 1058 |
|
1053 | 1059 | func (m *mockUExecutorQueryClient) GasPrice(ctx context.Context, req *uexecutortypes.QueryGasPriceRequest, opts ...grpc.CallOption) (*uexecutortypes.QueryGasPriceResponse, error) { |
@@ -1075,9 +1081,26 @@ func (m *mockUExecutorQueryClient) AllUniversalTx(ctx context.Context, req *uexe |
1075 | 1081 |
|
1076 | 1082 | func (m *mockUExecutorQueryClient) AllPendingOutbounds(ctx context.Context, req *uexecutortypes.QueryAllPendingOutboundsRequest, opts ...grpc.CallOption) (*uexecutortypes.QueryAllPendingOutboundsResponse, error) { |
1077 | 1083 | m.lastPendingReq = req |
| 1084 | + m.pendingReqs = append(m.pendingReqs, req) |
1078 | 1085 | if m.err != nil { |
1079 | 1086 | return nil, m.err |
1080 | 1087 | } |
| 1088 | + if m.pendingTotal > 0 { |
| 1089 | + offset := int(req.Pagination.GetOffset()) |
| 1090 | + end := offset + int(req.Pagination.GetLimit()) |
| 1091 | + if end > m.pendingTotal { |
| 1092 | + end = m.pendingTotal |
| 1093 | + } |
| 1094 | + resp := &uexecutortypes.QueryAllPendingOutboundsResponse{ |
| 1095 | + Pagination: &query.PageResponse{Total: uint64(m.pendingTotal)}, |
| 1096 | + } |
| 1097 | + for i := offset; i < end; i++ { |
| 1098 | + id := fmt.Sprintf("ob-%d", i) |
| 1099 | + resp.Entries = append(resp.Entries, &uexecutortypes.PendingOutboundEntry{OutboundId: id}) |
| 1100 | + resp.Outbounds = append(resp.Outbounds, &uexecutortypes.OutboundTx{Id: id}) |
| 1101 | + } |
| 1102 | + return resp, nil |
| 1103 | + } |
1081 | 1104 | return m.allPendingOutboundsResp, nil |
1082 | 1105 | } |
1083 | 1106 |
|
@@ -1171,54 +1194,74 @@ func TestClient_GetKeyByID(t *testing.T) { |
1171 | 1194 | }) |
1172 | 1195 | } |
1173 | 1196 |
|
1174 | | -// An outbound only leaves the pending set on a quorum vote, so one that cannot |
1175 | | -// reach a vote sits at the head of an oldest-first list permanently and hides |
1176 | | -// everything newer. New outbounds always arrive at the newest end, so reading |
1177 | | -// that end is what cannot be starved. |
1178 | | -func TestClient_GetAllPendingOutbounds_ReadsNewestFirst(t *testing.T) { |
| 1197 | +// An outbound leaves the pending set only on a quorum vote, so rows that cannot |
| 1198 | +// reach one accumulate at the head of an oldest-first list. The walk is what |
| 1199 | +// stops them hiding everything newer. |
| 1200 | +func TestClient_GetAllPendingOutbounds_WalksOldestFirst(t *testing.T) { |
1179 | 1201 | ctx := context.Background() |
1180 | 1202 |
|
1181 | | - resp := func(total uint64) *uexecutortypes.QueryAllPendingOutboundsResponse { |
1182 | | - return &uexecutortypes.QueryAllPendingOutboundsResponse{ |
1183 | | - Entries: []*uexecutortypes.PendingOutboundEntry{{OutboundId: "ob-1"}}, |
1184 | | - Outbounds: []*uexecutortypes.OutboundTx{{Id: "ob-1"}}, |
1185 | | - Pagination: &query.PageResponse{Total: total}, |
1186 | | - } |
| 1203 | + newClient := func(total int) (*Client, *mockUExecutorQueryClient) { |
| 1204 | + m := &mockUExecutorQueryClient{pendingTotal: total} |
| 1205 | + return &Client{logger: zerolog.Nop(), uexecutorClients: []uexecutortypes.QueryClient{m}}, m |
1187 | 1206 | } |
1188 | 1207 |
|
1189 | | - t.Run("reads the newest end, never an offset", func(t *testing.T) { |
1190 | | - mockClient := &mockUExecutorQueryClient{allPendingOutboundsResp: resp(9)} |
1191 | | - client := &Client{logger: zerolog.Nop(), uexecutorClients: []uexecutortypes.QueryClient{mockClient}} |
1192 | | - |
| 1208 | + t.Run("oldest first, never reversed", func(t *testing.T) { |
| 1209 | + client, m := newClient(9) |
1193 | 1210 | _, _, err := client.GetAllPendingOutbounds(ctx) |
1194 | 1211 | require.NoError(t, err) |
1195 | 1212 |
|
1196 | | - p := mockClient.lastPendingReq.Pagination |
| 1213 | + p := m.pendingReqs[0].Pagination |
1197 | 1214 | require.NotNil(t, p) |
1198 | | - assert.True(t, p.Reverse, "a stuck prefix at the oldest end must not hide newer rows") |
1199 | | - assert.Zero(t, p.Offset, "offset zero is the only position that cannot shift under insertion") |
1200 | | - assert.Equal(t, uint64(pendingOutboundLimit), p.Limit) |
| 1215 | + assert.False(t, p.Reverse, "older outbounds must be read first") |
| 1216 | + assert.Zero(t, p.Offset) |
| 1217 | + assert.Equal(t, uint64(pendingOutboundPageSize), p.Limit) |
1201 | 1218 | }) |
1202 | 1219 |
|
1203 | | - // Above the limit older rows stop being read. They are already known locally, |
1204 | | - // but an operator should still be told the set is that large. |
1205 | | - t.Run("reports a set larger than one request", func(t *testing.T) { |
| 1220 | + // The ordinary case is a set that fits, and it must not cost extra requests. |
| 1221 | + t.Run("a set that fits costs one request", func(t *testing.T) { |
| 1222 | + client, m := newClient(9) |
| 1223 | + entries, _, err := client.GetAllPendingOutbounds(ctx) |
| 1224 | + require.NoError(t, err) |
| 1225 | + assert.Len(t, m.pendingReqs, 1) |
| 1226 | + assert.Len(t, entries, 9) |
| 1227 | + }) |
| 1228 | + |
| 1229 | + // A stuck prefix must not hide what is behind it. |
| 1230 | + t.Run("walks past a full first page", func(t *testing.T) { |
| 1231 | + client, m := newClient(pendingOutboundPageSize + 250) |
| 1232 | + entries, outbounds, err := client.GetAllPendingOutbounds(ctx) |
| 1233 | + require.NoError(t, err) |
| 1234 | + |
| 1235 | + require.Len(t, m.pendingReqs, 2) |
| 1236 | + assert.Equal(t, uint64(0), m.pendingReqs[0].Pagination.GetOffset()) |
| 1237 | + assert.Equal(t, uint64(pendingOutboundPageSize), m.pendingReqs[1].Pagination.GetOffset()) |
| 1238 | + |
| 1239 | + require.Len(t, entries, pendingOutboundPageSize+250) |
| 1240 | + require.Len(t, outbounds, pendingOutboundPageSize+250) |
| 1241 | + assert.Equal(t, "ob-0", entries[0].OutboundId, "oldest first") |
| 1242 | + assert.Equal(t, fmt.Sprintf("ob-%d", pendingOutboundPageSize+249), entries[len(entries)-1].OutboundId) |
| 1243 | + }) |
| 1244 | + |
| 1245 | + // The cap bounds one poll; the rest is read on the next tick. |
| 1246 | + t.Run("stops at the page cap and says so", func(t *testing.T) { |
1206 | 1247 | var logBuf bytes.Buffer |
1207 | | - mockClient := &mockUExecutorQueryClient{allPendingOutboundsResp: resp(pendingOutboundLimit + 500)} |
1208 | | - client := &Client{logger: zerolog.New(&logBuf), uexecutorClients: []uexecutortypes.QueryClient{mockClient}} |
| 1248 | + m := &mockUExecutorQueryClient{pendingTotal: pendingOutboundPageSize * (pendingOutboundMaxPages + 2)} |
| 1249 | + client := &Client{logger: zerolog.New(&logBuf), uexecutorClients: []uexecutortypes.QueryClient{m}} |
1209 | 1250 |
|
1210 | | - _, _, err := client.GetAllPendingOutbounds(ctx) |
| 1251 | + entries, _, err := client.GetAllPendingOutbounds(ctx) |
1211 | 1252 | require.NoError(t, err) |
1212 | | - assert.Contains(t, logBuf.String(), "exceeds one request") |
| 1253 | + assert.Len(t, m.pendingReqs, pendingOutboundMaxPages) |
| 1254 | + assert.Len(t, entries, pendingOutboundPageSize*pendingOutboundMaxPages) |
| 1255 | + assert.Contains(t, logBuf.String(), "page cap reached") |
1213 | 1256 | }) |
1214 | 1257 |
|
1215 | 1258 | t.Run("quiet when the set fits", func(t *testing.T) { |
1216 | 1259 | var logBuf bytes.Buffer |
1217 | | - mockClient := &mockUExecutorQueryClient{allPendingOutboundsResp: resp(9)} |
1218 | | - client := &Client{logger: zerolog.New(&logBuf), uexecutorClients: []uexecutortypes.QueryClient{mockClient}} |
| 1260 | + m := &mockUExecutorQueryClient{pendingTotal: 9} |
| 1261 | + client := &Client{logger: zerolog.New(&logBuf), uexecutorClients: []uexecutortypes.QueryClient{m}} |
1219 | 1262 |
|
1220 | 1263 | _, _, err := client.GetAllPendingOutbounds(ctx) |
1221 | 1264 | require.NoError(t, err) |
1222 | | - assert.NotContains(t, logBuf.String(), "exceeds one request") |
| 1265 | + assert.NotContains(t, logBuf.String(), "page cap reached") |
1223 | 1266 | }) |
1224 | 1267 | } |
0 commit comments