github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/beacon/light/request/scheduler_test.go (about)

     1  package request
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestEventFilter(t *testing.T) {
     9  	s := NewScheduler()
    10  	module1 := &testModule{name: "module1"}
    11  	module2 := &testModule{name: "module2"}
    12  	s.RegisterModule(module1, "module1")
    13  	s.RegisterModule(module2, "module2")
    14  	s.Start()
    15  	// startup process round without events
    16  	s.testWaitCh <- struct{}{}
    17  	module1.expProcess(t, nil)
    18  	module2.expProcess(t, nil)
    19  	srv := &testServer{}
    20  	// register server; both modules should receive server event
    21  	s.RegisterServer(srv)
    22  	s.testWaitCh <- struct{}{}
    23  	module1.expProcess(t, []Event{
    24  		{Type: EvRegistered, Server: srv},
    25  	})
    26  	module2.expProcess(t, []Event{
    27  		{Type: EvRegistered, Server: srv},
    28  	})
    29  	// let module1 send a request
    30  	srv.canRequest = 1
    31  	module1.sendReq = testRequest
    32  	s.Trigger()
    33  	// in first triggered round module1 sends the request, no events yet
    34  	s.testWaitCh <- struct{}{}
    35  	module1.expProcess(t, nil)
    36  	module2.expProcess(t, nil)
    37  	// server emits EvTimeout; only module1 should receive it
    38  	srv.eventCb(Event{Type: EvTimeout, Data: RequestResponse{ID: 1, Request: testRequest}})
    39  	s.testWaitCh <- struct{}{}
    40  	module1.expProcess(t, []Event{
    41  		{Type: EvTimeout, Server: srv, Data: RequestResponse{ID: 1, Request: testRequest}},
    42  	})
    43  	module2.expProcess(t, nil)
    44  	// unregister server; both modules should receive server event
    45  	s.UnregisterServer(srv)
    46  	s.testWaitCh <- struct{}{}
    47  	module1.expProcess(t, []Event{
    48  		// module1 should also receive EvFail on its pending request
    49  		{Type: EvFail, Server: srv, Data: RequestResponse{ID: 1, Request: testRequest}},
    50  		{Type: EvUnregistered, Server: srv},
    51  	})
    52  	module2.expProcess(t, []Event{
    53  		{Type: EvUnregistered, Server: srv},
    54  	})
    55  	// response after server unregistered; should be discarded
    56  	srv.eventCb(Event{Type: EvResponse, Data: RequestResponse{ID: 1, Request: testRequest, Response: testResponse}})
    57  	s.testWaitCh <- struct{}{}
    58  	module1.expProcess(t, nil)
    59  	module2.expProcess(t, nil)
    60  	// no more process rounds expected; shut down
    61  	s.testWaitCh <- struct{}{}
    62  	module1.expNoMoreProcess(t)
    63  	module2.expNoMoreProcess(t)
    64  	s.Stop()
    65  }
    66  
    67  type testServer struct {
    68  	eventCb    func(Event)
    69  	lastID     ID
    70  	canRequest int
    71  }
    72  
    73  func (s *testServer) Name() string {
    74  	return ""
    75  }
    76  
    77  func (s *testServer) subscribe(eventCb func(Event)) {
    78  	s.eventCb = eventCb
    79  }
    80  
    81  func (s *testServer) canRequestNow() bool {
    82  	return s.canRequest > 0
    83  }
    84  
    85  func (s *testServer) sendRequest(req Request) ID {
    86  	s.canRequest--
    87  	s.lastID++
    88  	return s.lastID
    89  }
    90  
    91  func (s *testServer) fail(string)  {}
    92  func (s *testServer) unsubscribe() {}
    93  
    94  type testModule struct {
    95  	name      string
    96  	processed [][]Event
    97  	sendReq   Request
    98  }
    99  
   100  func (m *testModule) Process(requester Requester, events []Event) {
   101  	m.processed = append(m.processed, events)
   102  	if m.sendReq != nil {
   103  		if cs := requester.CanSendTo(); len(cs) > 0 {
   104  			requester.Send(cs[0], m.sendReq)
   105  		}
   106  	}
   107  }
   108  
   109  func (m *testModule) expProcess(t *testing.T, expEvents []Event) {
   110  	if len(m.processed) == 0 {
   111  		t.Errorf("Missing call to %s.Process", m.name)
   112  		return
   113  	}
   114  	events := m.processed[0]
   115  	m.processed = m.processed[1:]
   116  	if !reflect.DeepEqual(events, expEvents) {
   117  		t.Errorf("Call to %s.Process with wrong events (expected %v, got %v)", m.name, expEvents, events)
   118  	}
   119  }
   120  
   121  func (m *testModule) expNoMoreProcess(t *testing.T) {
   122  	for len(m.processed) > 0 {
   123  		t.Errorf("Unexpected call to %s.Process with events %v", m.name, m.processed[0])
   124  		m.processed = m.processed[1:]
   125  	}
   126  }