Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0758e24

Browse files
committed
chore: add broker for test databases
1 parent 6d39077 commit 0758e24

File tree

7 files changed

+1285
-0
lines changed

7 files changed

+1285
-0
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ GEN_FILES := \
636636
provisionersdk/proto/provisioner.pb.go \
637637
provisionerd/proto/provisionerd.pb.go \
638638
vpn/vpn.pb.go \
639+
coderd/database/dbtestutil/broker/broker.pb.go \
639640
$(DB_GEN_FILES) \
640641
$(SITE_GEN_FILES) \
641642
coderd/rbac/object_gen.go \
@@ -683,6 +684,7 @@ gen/mark-fresh:
683684
provisionersdk/proto/provisioner.pb.go \
684685
provisionerd/proto/provisionerd.pb.go \
685686
vpn/vpn.pb.go \
687+
coderd/database/dbtestutil/broker/broker.pb.go \
686688
coderd/database/dump.sql \
687689
$(DB_GEN_FILES) \
688690
site/src/api/typesGenerated.ts \
@@ -801,6 +803,14 @@ vpn/vpn.pb.go: vpn/vpn.proto
801803
--go_opt=paths=source_relative \
802804
./vpn/vpn.proto
803805

806+
coderd/database/dbtestutil/broker/broker.pb.go: coderd/database/dbtestutil/broker/broker.proto
807+
protoc \
808+
--go_out=. \
809+
--go_opt=paths=source_relative \
810+
--go-drpc_out=. \
811+
--go-drpc_opt=paths=source_relative \
812+
./coderd/database/dbtestutil/broker/broker.proto
813+
804814
site/src/api/typesGenerated.ts: site/node_modules/.installed $(wildcard scripts/apitypings/*) $(shell find ./codersdk $(FIND_EXCLUSIONS) -type f -name '*.go')
805815
# -C sets the directory for the go run command
806816
go run -C ./scripts/apitypings main.go > $@

cmd/dbtestbroker/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
// dbtestbroker is a subprocess used when testing with dbtestutil. It handles cloning Postgres databases for tests and
4+
// is a separate process so that if the main test process dies (panics, times out, or is killed), we still clean up
5+
// the test databases and don't leave them in Postgres.
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"io"
11+
"os"
12+
"os/signal"
13+
"time"
14+
15+
_ "github.com/lib/pq"
16+
"golang.org/x/xerrors"
17+
18+
"github.com/coder/coder/v2/coderd/database/dbtestutil/broker"
19+
)
20+
21+
func main() {
22+
connectCtx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
23+
defer cancel()
24+
server, err := broker.NewService(connectCtx, os.Stdin, os.Stdout)
25+
if err != nil {
26+
_, _ = fmt.Fprintf(os.Stderr, "\nDBTESTBROKER: %s\n", err.Error())
27+
// nolint: exitAfterDefer
28+
os.Exit(1)
29+
}
30+
signalCtx, signalCancel := signal.NotifyContext(context.Background(), os.Interrupt)
31+
defer signalCancel()
32+
err = server.Serve(signalCtx)
33+
if err != nil && !xerrors.Is(err, context.Canceled) && !xerrors.Is(err, io.EOF) {
34+
_, _ = fmt.Fprintf(os.Stderr, "\nDBTESTBROKER: %s\n", err.Error())
35+
}
36+
}

0 commit comments

Comments
 (0)