|
6 | 6 | "testing" |
7 | 7 | "time" |
8 | 8 |
|
| 9 | + "github.com/creachadair/jrpc2" |
9 | 10 | "github.com/stretchr/testify/assert" |
10 | 11 | "github.com/stretchr/testify/mock" |
11 | 12 |
|
@@ -210,6 +211,73 @@ func TestGetLedgerBeyondLatest(t *testing.T) { |
210 | 211 |
|
211 | 212 | } |
212 | 213 |
|
| 214 | +func TestGetLedgerBeyondLatestBasedOnDurationLimitErr(t *testing.T) { |
| 215 | + rpcBackend, mockClient := setupRPCTest(t) |
| 216 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 217 | + defer cancel() |
| 218 | + requestedSequence := uint32(100) |
| 219 | + |
| 220 | + rpcGetLedgersRequest := protocol.GetLedgersRequest{ |
| 221 | + StartLedger: requestedSequence, |
| 222 | + Pagination: &protocol.LedgerPaginationOptions{ |
| 223 | + Limit: uint(rpcBackendDefaultBufferSize), |
| 224 | + }, |
| 225 | + } |
| 226 | + // Setup err responses indicating rpc request elapsed beyond request duration |
| 227 | + // triggering specific error codes related to request duration limits which rpc may emit |
| 228 | + exceededProcessingLimitThreshold := jrpc2.Error{ |
| 229 | + Code: -32001, |
| 230 | + } |
| 231 | + |
| 232 | + failToProcessDueToInternalIssue := jrpc2.Error{ |
| 233 | + Code: -32003, |
| 234 | + } |
| 235 | + |
| 236 | + // called by PrepareRange |
| 237 | + mockClient.On("GetLedgers", ctx, rpcGetLedgersRequest).Return(protocol.GetLedgersResponse{}, exceededProcessingLimitThreshold).Once() |
| 238 | + // called by GetLedger first time |
| 239 | + mockClient.On("GetLedgers", ctx, rpcGetLedgersRequest).Return(protocol.GetLedgersResponse{}, failToProcessDueToInternalIssue).Once() |
| 240 | + |
| 241 | + // Setup second call to return the requested ledger |
| 242 | + lcm := xdr.LedgerCloseMeta{ |
| 243 | + V: 0, |
| 244 | + V0: &xdr.LedgerCloseMetaV0{ |
| 245 | + LedgerHeader: xdr.LedgerHeaderHistoryEntry{ |
| 246 | + Header: xdr.LedgerHeader{ |
| 247 | + LedgerSeq: xdr.Uint32(requestedSequence), |
| 248 | + }, |
| 249 | + }, |
| 250 | + }, |
| 251 | + } |
| 252 | + encodedLCM, err := xdr.MarshalBase64(lcm) |
| 253 | + assert.NoError(t, err) |
| 254 | + |
| 255 | + secondResponse := protocol.GetLedgersResponse{ |
| 256 | + LatestLedger: requestedSequence, |
| 257 | + Ledgers: []protocol.LedgerInfo{ |
| 258 | + { |
| 259 | + Sequence: requestedSequence, |
| 260 | + LedgerMetadata: encodedLCM, |
| 261 | + }, |
| 262 | + }, |
| 263 | + } |
| 264 | + // called by GetLedger second time |
| 265 | + mockClient.On("GetLedgers", ctx, rpcGetLedgersRequest).Return(secondResponse, nil).Once() |
| 266 | + |
| 267 | + preparedRange := Range{from: requestedSequence, to: requestedSequence + 10, bounded: true} |
| 268 | + rpcBackend.PrepareRange(ctx, preparedRange) |
| 269 | + |
| 270 | + startTime := time.Now() |
| 271 | + actualLCM, err := rpcBackend.GetLedger(ctx, requestedSequence) |
| 272 | + duration := time.Since(startTime) |
| 273 | + |
| 274 | + assert.NoError(t, err) |
| 275 | + assert.Equal(t, requestedSequence, uint32(actualLCM.V0.LedgerHeader.Header.LedgerSeq)) |
| 276 | + |
| 277 | + // Verify timing - GetLedger should have waited one interval and then refetched ledgers from rpc on second call |
| 278 | + assert.GreaterOrEqual(t, duration.Seconds(), float64(rpcBackendDefaultWaitIntervalSeconds)) |
| 279 | +} |
| 280 | + |
213 | 281 | func TestGetLedgerContextTimeout(t *testing.T) { |
214 | 282 | rpcBackend, mockClient := setupRPCTest(t) |
215 | 283 | sequence := uint32(100) |
|
0 commit comments