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

Skip to content

Commit ef3db0e

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

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
"golang.org/x/xerrors"
16+
17+
"github.com/coder/coder/v2/coderd/database/dbtestutil/broker"
18+
)
19+
20+
func main() {
21+
connectCtx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
22+
defer cancel()
23+
server, err := broker.NewService(connectCtx, os.Stdin, os.Stdout)
24+
if err != nil {
25+
_, _ = fmt.Fprintf(os.Stderr, "\nDBTESTBROKER: %s\n", err.Error())
26+
cancel()
27+
os.Exit(1)
28+
}
29+
signalCtx, signalCancel := signal.NotifyContext(context.Background(), os.Interrupt)
30+
defer signalCancel()
31+
err = server.Serve(signalCtx)
32+
if err != nil && !xerrors.Is(err, context.Canceled) && !xerrors.Is(err, io.EOF) {
33+
_, _ = fmt.Fprintf(os.Stderr, "\nDBTESTBROKER: %s\n", err.Error())
34+
}
35+
}

0 commit comments

Comments
 (0)