github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/rpc/query/query_test.go (about) 1 //go:build integration 2 // +build integration 3 4 // Copyright 2021 The TrueBlocks Authors. All rights reserved. 5 // Use of this source code is governed by a license that can 6 // be found in the LICENSE file. 7 8 package query 9 10 import ( 11 "encoding/json" 12 "net/http" 13 "net/http/httptest" 14 "testing" 15 16 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils" 17 ) 18 19 func TestFromRpcCounter(t *testing.T) { 20 ids := []int{} 21 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 22 var payload struct { 23 ID int 24 } 25 err := json.NewDecoder(r.Body).Decode(&payload) 26 if err != nil { 27 t.Fatal(err) 28 } 29 ids = append(ids, payload.ID) 30 w.Write([]byte("{}")) 31 })) 32 defer server.Close() 33 34 for i := 0; i < 20; i++ { 35 payload := Payload{ 36 Method: "eth_blockNumber", 37 Params: []interface{}{}, 38 } 39 _, err := Query[string](utils.GetTestChain(), payload.Method, payload.Params) 40 if err != nil { 41 t.Fatal(err) 42 } 43 } 44 45 seen := map[int]bool{} 46 for index, id := range ids { 47 if index != len(ids)-1 && ids[index+1]-id != 1 { 48 t.Fatal("ids should be in order", id, ids[index+1]) 49 } 50 if id == 0 { 51 t.Fatal("0 not allowed") 52 } 53 alreadySeen, ok := seen[id] 54 if ok && alreadySeen { 55 t.Fatal("duplicated id", id) 56 } 57 } 58 }