github.com/ethereum-optimism/optimism@v1.7.2/op-node/heartbeat/service_test.go (about) 1 package heartbeat 2 3 import ( 4 "context" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/ethereum/go-ethereum/log" 14 ) 15 16 const expHeartbeat = `{ 17 "version": "v1.2.3", 18 "meta": "meta", 19 "moniker": "yeet", 20 "peerID": "1UiUfoobar", 21 "chainID": 1234 22 }` 23 24 func TestBeat(t *testing.T) { 25 ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) 26 defer cancel() 27 28 reqCh := make(chan string, 2) 29 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 30 w.WriteHeader(204) 31 body, err := io.ReadAll(r.Body) 32 require.NoError(t, err) 33 reqCh <- string(body) 34 r.Body.Close() 35 })) 36 defer s.Close() 37 38 doneCh := make(chan struct{}) 39 go func() { 40 _ = Beat(ctx, log.Root(), s.URL, &Payload{ 41 Version: "v1.2.3", 42 Meta: "meta", 43 Moniker: "yeet", 44 PeerID: "1UiUfoobar", 45 ChainID: 1234, 46 }) 47 doneCh <- struct{}{} 48 }() 49 50 select { 51 case hb := <-reqCh: 52 require.JSONEq(t, expHeartbeat, hb) 53 cancel() 54 <-doneCh 55 case <-ctx.Done(): 56 t.Fatalf("error: %v", ctx.Err()) 57 } 58 }