Files
tecamino-dbm/test/dbm_test.go
Adrian Zürcher d515e2d9d7
All checks were successful
Build DBM Service / build (amd64, , linux) (push) Successful in 2m2s
Build DBM Service / build (amd64, .exe, windows) (push) Successful in 1m52s
Build DBM Service / build (arm, 6, , linux) (push) Successful in 1m57s
Build DBM Service / build (arm64, , linux) (push) Successful in 1m52s
change selfsigned cert to work with firefox
2026-02-19 08:41:15 +01:00

177 lines
3.6 KiB
Go

package test
import (
"fmt"
"math/rand"
"os"
"runtime"
"testing"
"time"
"gitea.tecamino.com/paadi/tecamino-dbm/args"
"gitea.tecamino.com/paadi/tecamino-dbm/cert"
"gitea.tecamino.com/paadi/tecamino-dbm/dbm"
"gitea.tecamino.com/paadi/tecamino-dbm/models"
"gitea.tecamino.com/paadi/tecamino-dbm/utils"
ws "gitea.tecamino.com/paadi/tecamino-dbm/websocket"
"github.com/google/uuid"
)
func TestCreateDps(t *testing.T) {
dmaHandler, err := dbm.NewDbmHandler(&args.Args{
Port: models.Port{
Http: 8100,
Https: 8101,
},
Cert: cert.Cert{
Organization: "tecamino",
CertFile: "./cert/cert.pem",
KeyFile: "./cert/key.pem",
},
RootDir: ".",
DBMFile: "Test",
Debug: false,
})
if err != nil {
t.Fatal(err)
}
rand.NewSource(time.Now().UnixNano())
ndps := utils.ListofA2ZZ()
l := len(ndps)
s := time.Now()
// for _, dp := range ndps {
// for i := 0; i < 100; i++ {
// err = dmaHandler.ImportDatapoints(&models.Datapoint{
// Path: fmt.Sprintf("Test:%s:%03d", dp, i),
// Type: models.RandomType(),
// Value: rand.Int31(),
// })
// if err != nil {
// t.Fatal(err)
// }
// }
// }
fmt.Printf("time used to create %d datapoints: %v\n", l*100, time.Since(s))
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("Allocated: %.2f MB\n", float64(m.Alloc)/1024/1024)
fmt.Printf("Total Allocated (ever): %.2f MB\n", float64(m.TotalAlloc)/1024/1024)
fmt.Printf("System Memory Obtained: %.2f MB\n", float64(m.Sys)/1024/1024)
fmt.Printf("Heap In Use: %.2f MB\n", float64(m.HeapInuse)/1024/1024)
fmt.Printf("GC Runs: %d\n", m.NumGC)
err = dmaHandler.SaveDb()
if err != nil {
t.Fatal(err)
}
}
func TestQuery(t *testing.T) {
dmaHandler, err := dbm.NewDbmHandler(&args.Args{
Port: models.Port{
Http: 8100,
Https: 8101,
},
Cert: cert.Cert{
Organization: "tecamino",
CertFile: "./cert/cert.pem",
KeyFile: "./cert/key.pem",
},
RootDir: ".",
DBMFile: "Test",
Debug: false,
})
if err != nil {
panic(err)
}
for i, o := range dmaHandler.DBM.QueryDatapoints(1, uuid.Nil, "Test:A:000") {
fmt.Println(600, i, o)
}
}
func TestUpdateDps(t *testing.T) {
dmaHandler, err := dbm.NewDbmHandler(&args.Args{
Port: models.Port{
Http: 8100,
Https: 8101,
},
Cert: cert.Cert{
Organization: "tecamino",
CertFile: "./cert/cert.pem",
KeyFile: "./cert/key.pem",
},
RootDir: ".",
DBMFile: "Test",
Debug: false,
})
if err != nil {
t.Fatal(err)
}
rand.NewSource(time.Now().UnixNano())
ndps := utils.ListofA2ZZ()
l := len(ndps)
s := time.Now()
// for j, dp := range ndps {
// if j > 2 {
// break
// }
// for i := 0; i < 100; i++ {
// err = dmaHandler.UpdateDatapointValue(fmt.Sprintf("Test:%s:%03d", dp, i), rand.Int31())
// if err != nil {
// t.Fatal(err)
// }
// }
// }
fmt.Printf("time used to update %d datapoints: %v\n", l*100, time.Since(s))
time.Sleep(5 * time.Second)
fmt.Println("save data")
err = dmaHandler.SaveDb()
if err != nil {
t.Fatal(err)
}
}
func TestServer(t *testing.T) {
fmt.Println("start")
server := ws.NewServer([]string{".*"}, 9500)
t.Fatal(server.ServeHttp("http://localhost", 8100))
}
func TestCreateCerts(t *testing.T) {
certFile := "cert.pem"
keyFile := "key.pem"
defer func() {
err := os.Remove(certFile)
if err != nil {
t.Fatal(err)
}
err = os.Remove(keyFile)
if err != nil {
t.Fatal(err)
}
}()
t.Log("test create certificates")
cert := cert.Cert{
Organization: "tecamino",
CertFile: certFile,
KeyFile: keyFile,
}
if err := cert.GenerateSelfSignedCert(); err != nil {
t.Fatal(err)
}
}