From 1a9dde2ddf381556f178afedb7f7b32c83c3b4dc Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Sat, 28 Sep 2024 15:28:49 +0200 Subject: [PATCH] succd: add ci, tests --- .forgejo/workflows/succd.yaml | 8 +++ succbone/succd/ci.sh | 30 +++++++++++ succbone/succd/process_blocks_test.go | 72 +++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 .forgejo/workflows/succd.yaml create mode 100755 succbone/succd/ci.sh create mode 100644 succbone/succd/process_blocks_test.go diff --git a/.forgejo/workflows/succd.yaml b/.forgejo/workflows/succd.yaml new file mode 100644 index 0000000..0a5c1e6 --- /dev/null +++ b/.forgejo/workflows/succd.yaml @@ -0,0 +1,8 @@ +on: [push] +jobs: + test: + runs-on: docker + container: golang:1.23-bookworm + steps: + - uses: https://github.com/actions/checkout@v4 + - run: cd succbone/succd && ./ci.sh diff --git a/succbone/succd/ci.sh b/succbone/succd/ci.sh new file mode 100755 index 0000000..2361b73 --- /dev/null +++ b/succbone/succd/ci.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +set -e +set -o pipefail + +if [ ! -f go.mod ]; then + echo "Script needs to be run with in succd directory." + exit 1 +fi + +echo "Running gofmt..." +GOFMT_OUT="$(gofmt -d $(find . -name '*.go'))" +if [ -n "$GOFMT_OUT" ]; then + echo "gofmt generated differences, please run 'go fmt ./' and try again." + echo "$GOFMT_OUT" + exit 1 +fi + +echo "Running govet..." +GOVET_OUT="$(go vet ./)" +if [ -n "$GOVET_OUT" ]; then + echo "go vet found the following errors:" + echo "$GOVET_OUT" + exit 1 +fi + +echo "Running tests..." +go test ./ + +echo "OK." + diff --git a/succbone/succd/process_blocks_test.go b/succbone/succd/process_blocks_test.go new file mode 100644 index 0000000..4dd028c --- /dev/null +++ b/succbone/succd/process_blocks_test.go @@ -0,0 +1,72 @@ +package main + +import ( + "math" + "testing" + "time" +) + +func TestMomentaryOutput(t *testing.T) { + mo := momentaryOutput{} + mo.trigger() + mo.process() + if !mo.output { + t.Fatalf("output didn't trigger") + } + mo.process() + if !mo.output { + t.Fatalf("output didn't keep triggered") + } + time.Sleep(time.Second) + mo.process() + if mo.output { + t.Fatalf("output didn't untrigger") + } +} + +func TestThresholdOutput(t *testing.T) { + to := thresholdOutput{ + threshold: 1, + } + to.process(0) + if to.output { + t.Fatalf("output shouldn't have triggered") + } + to.process(2) + if !to.output { + t.Fatalf("output should have triggered") + } + to.process(0) + if !to.output { + t.Fatalf("output should have triggered (in debounce)") + } +} + +func TestRingbufferInput(t *testing.T) { + ri := ringbufferInput{ + limit: 3, + } + if ri.saturated() { + t.Fatal("ringbuffer shouldn't be saturated yet") + } + ri.process(1) + if ri.saturated() { + t.Fatal("ringbuffer shouldn't be saturated yet") + } + ri.process(2) + if ri.saturated() { + t.Fatal("ringbuffer shouldn't be saturated yet") + } + ri.process(3) + if !ri.saturated() { + t.Fatalf("ringbuffer should be saturated") + } + + if diff := math.Abs(float64(ri.avg) - 2); diff > 0.01 { + t.Fatalf("ringbuffer avg should be ~2, is %f", ri.avg) + } + ri.process(10) + if diff := math.Abs(float64(ri.avg) - 5); diff > 0.01 { + t.Fatalf("ringbuffer avg should be ~5, is %f", ri.avg) + } +}