diff --git a/.github/workflows/localnet-test.yml b/.github/workflows/localnet-test.yml index b9ac2d391..d793f6d93 100644 --- a/.github/workflows/localnet-test.yml +++ b/.github/workflows/localnet-test.yml @@ -41,8 +41,11 @@ jobs: - name: "get localnet stats" env: + # give the tests 5 minutes to run HEMI_E2E_DUMP_JSON_AFTER_MS: '300000' working-directory: ./e2e/monitor + # assert that: + # - we have at least 8 pop txs run: | - [[ $(go run ./... | jq '.["pop_tx_count"]') -gt 8 ]] && echo 'passing' || exit 1 + go test ./... diff --git a/e2e/monitor/main.go b/e2e/monitor/main.go index 8a24b3db8..d6e12fdea 100644 --- a/e2e/monitor/main.go +++ b/e2e/monitor/main.go @@ -19,7 +19,7 @@ import ( ) const ( - dataRefreshSeconds = 5 + dataRefreshSeconds = 1 tableRefreshSeconds = 1 ) @@ -34,6 +34,11 @@ type state struct { } func main() { + output := monitor(dumpJsonAfterMsFromEnv()) + fmt.Println(output) +} + +func monitor(dumpJsonAfterMs uint) string { s := state{ bitcoinBlockCount: 0, } @@ -48,25 +53,28 @@ func main() { go monitorPopTxs(ctx, &s, &mtx) go monitorRolledUpTxs(ctx, &s, &mtx) - if dumpJsonAfterMs() == 0 { + if dumpJsonAfterMs == 0 { go render(ctx, &s, t, &mtx) } - if dumpJsonAfterMs() > 0 { + if dumpJsonAfterMs > 0 { select { - case <-time.After(time.Duration(dumpJsonAfterMs()) * time.Millisecond): + case <-time.After(time.Duration(dumpJsonAfterMs) * time.Millisecond): output := dumpJson(&mtx, &s) - fmt.Println(output) + return output case <-ctx.Done(): } } else { for { select { case <-ctx.Done(): + return "" default: } } } + + return "" } type jsonOutput struct { @@ -316,7 +324,7 @@ func monitorRolledUpTxs(ctx context.Context, s *state, mtx *sync.Mutex) { } -func dumpJsonAfterMs() uint { +func dumpJsonAfterMsFromEnv() uint { val := os.Getenv("HEMI_E2E_DUMP_JSON_AFTER_MS") if val == "" { return 0 diff --git a/e2e/monitor/main_test.go b/e2e/monitor/main_test.go new file mode 100644 index 000000000..3e611c2d6 --- /dev/null +++ b/e2e/monitor/main_test.go @@ -0,0 +1,47 @@ +package main + +import ( + "encoding/json" + "math/big" + "testing" + + "github.com/hemilabs/heminetwork/hemi" +) + +func Test_Monitor(t *testing.T) { + ms := 1000 * 60 * 5 // dump after 5 minutes + seconds := ms / 1000 + + output := monitor(uint(ms)) + + var jo jsonOutput + + if err := json.Unmarshal([]byte(output), &jo); err != nil { + t.Fatal(err) + } + + popTxsPer100Seconds := 4 + expectedPopTxs := popTxsPer100Seconds * (seconds / 100) + + t.Logf("expecting at least %d pop txs mined", expectedPopTxs) + + if jo.PopTxCount < uint64(expectedPopTxs) { + t.Fatalf("popTxCount %d < %d", jo.PopTxCount, expectedPopTxs) + } + + popMinerBalance := big.NewInt(0) + balance, ok := popMinerBalance.SetString(jo.PopMinerHemiBalance, 10) + if !ok { + t.Fatalf("could not parse balance from %s", jo.PopMinerHemiBalance) + } + + expectedPayouts := expectedPopTxs - 8 + expectedPayoutBalance := big.NewInt(hemi.HEMIBase) + expectedPayoutBalance = expectedPayoutBalance.Mul(big.NewInt(int64(expectedPayouts)), expectedPayoutBalance) + + t.Logf("expecting actual balance %d to be greater than %d", balance, expectedPayoutBalance) + + if expectedPayoutBalance.Cmp(balance) > 0 { + t.Fatalf("pop miner payout balance %d < %d", expectedPayoutBalance, balance) + } +}