@@ -41,6 +41,7 @@ func newBatchTestService() *Service {
4141 Service : service.Service {
4242 Logger : slog .New (slog .NewTextHandler (& bytes.Buffer {}, nil )),
4343 },
44+ handlers : cloneDispatchTable (jsonrpcHandlers ),
4445 }
4546}
4647
@@ -128,7 +129,7 @@ func TestJSONRPCBatchRejectsMoreThanMaximumBeforeDispatch(t *testing.T) {
128129 s := newBatchTestService ()
129130 var calls atomic.Int32
130131 const method = "test_batch_cap"
131- withTestRPCHandler (t , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
132+ withTestRPCHandler (t , s , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
132133 calls .Add (1 )
133134 return true , nil
134135 })
@@ -312,7 +313,7 @@ func TestJSONRPCBatchReplacesResponsesAtCumulativeResponseBudget(t *testing.T) {
312313 var calls atomic.Int32
313314 const method = "test_large_batch_result"
314315 largeResult := strings .Repeat ("x" , testLargeResultSize )
315- withTestRPCHandler (t , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
316+ withTestRPCHandler (t , s , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
316317 calls .Add (1 )
317318 return largeResult , nil
318319 })
@@ -351,7 +352,7 @@ func TestJSONRPCBatchStopsBetweenEntriesWhenContextIsCanceled(t *testing.T) {
351352 s .Logger = slog .New (slog .NewJSONHandler (& logs , & slog.HandlerOptions {Level : slog .LevelDebug }))
352353 ctx , cancel := context .WithCancel (context .Background ())
353354 const method = "test_cancel_batch"
354- withTestRPCHandler (t , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
355+ withTestRPCHandler (t , s , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
355356 calls .Add (1 )
356357 cancel ()
357358 return true , nil
@@ -387,7 +388,7 @@ func TestJSONRPCBatchStopsSilentlyWhenRepositoryCallIsCanceled(t *testing.T) {
387388 s .Logger = slog .New (slog .NewJSONHandler (& logs , & slog.HandlerOptions {Level : slog .LevelDebug }))
388389 ctx , cancel := context .WithCancel (context .Background ())
389390 const method = "test_repository_cancel_batch"
390- withTestRPCHandler (t , method , func (s * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
391+ withTestRPCHandler (t , s , method , func (s * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
391392 calls .Add (1 )
392393 cancel ()
393394 return nil , s .repositoryError ("Unable to retrieve test data from repository" ,
@@ -419,7 +420,7 @@ func TestJSONRPCBatchReturnsErrorsForIDDRequestsAfterDeadline(t *testing.T) {
419420 s := newBatchTestService ()
420421 var calls atomic.Int32
421422 const method = "test_deadline_batch"
422- withTestRPCHandler (t , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
423+ withTestRPCHandler (t , s , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
423424 calls .Add (1 )
424425 return true , nil
425426 })
@@ -462,10 +463,10 @@ func TestJSONRPCBatchRecoversPanicPerEntry(t *testing.T) {
462463 panicMethod = "test_panic_batch"
463464 okMethod = "test_after_panic_batch"
464465 )
465- withTestRPCHandler (t , panicMethod , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
466+ withTestRPCHandler (t , s , panicMethod , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
466467 panic ("test panic" )
467468 })
468- withTestRPCHandler (t , okMethod , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
469+ withTestRPCHandler (t , s , okMethod , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
469470 return "ok" , nil
470471 })
471472
@@ -496,7 +497,7 @@ func TestJSONRPCBatchUsesOneAdmissionPermit(t *testing.T) {
496497 }
497498 var nestedAcquisitions atomic.Int32
498499 const method = "test_batch_admission"
499- withTestRPCHandler (t , method , func (s * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
500+ withTestRPCHandler (t , s , method , func (s * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
500501 if s .admission .TryAcquire () {
501502 nestedAcquisitions .Add (1 )
502503 s .admission .Release ()
@@ -578,15 +579,31 @@ func TestJSONRPCBatchMethodLoggingIsTruncated(t *testing.T) {
578579 require .True (t , found )
579580}
580581
581- func withTestRPCHandler (t * testing.T , method string , handler rpcHandler ) {
582+ func withTestRPCHandler (t * testing.T , service * Service , method string , handler rpcHandler ) {
582583 t .Helper ()
583- previous , existed := jsonrpcHandlers [method ]
584- jsonrpcHandlers [method ] = handler
584+ previous , existed := service . handlers [method ]
585+ service . handlers [method ] = handler
585586 t .Cleanup (func () {
586587 if existed {
587- jsonrpcHandlers [method ] = previous
588+ service . handlers [method ] = previous
588589 } else {
589- delete (jsonrpcHandlers , method )
590+ delete (service . handlers , method )
590591 }
591592 })
592593}
594+
595+ func TestRPCHandlerOverridesAreServiceLocal (t * testing.T ) {
596+ first := newBatchTestService ()
597+ second := newBatchTestService ()
598+ const method = "test_service_local_handler"
599+ withTestRPCHandler (t , first , method , func (_ * Service , _ * http.Request , _ RPCRequest ) (any , error ) {
600+ return true , nil
601+ })
602+
603+ _ , firstHasHandler := first .handlers [method ]
604+ _ , secondHasHandler := second .handlers [method ]
605+ _ , globalHasHandler := jsonrpcHandlers [method ]
606+ require .True (t , firstHasHandler )
607+ require .False (t , secondHasHandler )
608+ require .False (t , globalHasHandler )
609+ }
0 commit comments