fix(store): close pool on failed Ping; add ListEndpoints test

This commit is contained in:
2026-07-01 16:55:24 +07:00
parent 420358b558
commit 0cf9de38c4
2 changed files with 21 additions and 0 deletions
+1
View File
@@ -16,6 +16,7 @@ func New(ctx context.Context, dsn string) (*Store, error) {
return nil, err
}
if err := pool.Ping(ctx); err != nil {
pool.Close()
return nil, err
}
return &Store{Pool: pool}, nil
+20
View File
@@ -38,3 +38,23 @@ func TestCreateAndGetEndpoint(t *testing.T) {
t.Fatalf("got %+v", got)
}
}
func TestListEndpointsOrdered(t *testing.T) {
s := testStore(t)
ctx := context.Background()
id1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "src", Host: "a.com", Port: 993, TLSMode: "ssl"})
id2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "dst", Host: "b.com", Port: 143, TLSMode: "starttls"})
eps, err := s.ListEndpoints(ctx)
if err != nil {
t.Fatalf("list: %v", err)
}
if len(eps) != 2 {
t.Fatalf("len=%d want 2", len(eps))
}
if eps[0].ID != id1 || eps[1].ID != id2 {
t.Fatalf("order wrong: %d,%d want %d,%d", eps[0].ID, eps[1].ID, id1, id2)
}
if eps[1].TLSMode != "starttls" {
t.Fatalf("eps[1].TLSMode=%q want starttls", eps[1].TLSMode)
}
}