aboutsummaryrefslogtreecommitdiff
path: root/lilin_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-07-17 02:22:30 +0700
committerShulhan <ms@kilabit.info>2025-07-17 02:30:40 +0700
commit4d4b26dff03083955871439ee6e0ef9bd07c5a1c (patch)
treeab239b115d9e39a2a7d8befd3ee37d101afb0c17 /lilin_test.go
downloadlilin-4d4b26dff03083955871439ee6e0ef9bd07c5a1c.tar.xz
lilin: simple service monitoring
lilin is a program to help monitor other services through HTTP, TCP, and UDP connection.
Diffstat (limited to 'lilin_test.go')
-rw-r--r--lilin_test.go89
1 files changed, 89 insertions, 0 deletions
diff --git a/lilin_test.go b/lilin_test.go
new file mode 100644
index 0000000..804bc3c
--- /dev/null
+++ b/lilin_test.go
@@ -0,0 +1,89 @@
+// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-only
+
+package lilin_test
+
+import (
+ "context"
+ "errors"
+ "log"
+ "net/http"
+ "testing"
+ "time"
+
+ "git.sr.ht/~shulhan/lilin"
+ "git.sr.ht/~shulhan/pakakeh.go/lib/net"
+ "git.sr.ht/~shulhan/pakakeh.go/lib/test"
+)
+
+var client *lilin.Client
+
+func TestMain(m *testing.M) {
+ var server *lilin.Server
+
+ server = startServer()
+ client = createClient(server)
+
+ m.Run()
+
+ var ctx context.Context
+ var cancelfn context.CancelFunc
+ ctx, cancelfn = context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancelfn()
+
+ var err = server.Shutdown(ctx)
+ if err != nil {
+ log.Fatal(err)
+ }
+}
+
+func startServer() (server *lilin.Server) {
+ var serverOpts = lilin.ServerOptions{
+ BaseDir: `testdata`,
+ }
+ var err error
+
+ server, err = lilin.NewServer(serverOpts)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ go func() {
+ var err2 = server.ListenAndServe()
+ if err2 != nil && !errors.Is(err2, http.ErrServerClosed) {
+ log.Fatal(err2)
+ }
+ }()
+
+ err = net.WaitAlive(`tcp`, server.Options.Address, 5*time.Second)
+ if err != nil {
+ log.Fatal(err)
+ }
+ return server
+}
+
+func createClient(server *lilin.Server) (client *lilin.Client) {
+ var clientOpts = lilin.ClientOptions{
+ ServerURL: `http://` + server.Options.Address,
+ }
+ var err error
+
+ client, err = lilin.NewClient(clientOpts)
+ if err != nil {
+ log.Fatal(err)
+ }
+ return client
+}
+
+func TestServer_handleServicesSummary(t *testing.T) {
+ var gotSummary []lilin.Service
+ var err error
+
+ gotSummary, err = client.ServicesSummary()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var expSummary []lilin.Service
+ test.Assert(t, `ServicesSummary`, expSummary, gotSummary)
+}