Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Mar 7, 2024
1 parent c424278 commit 187e4b6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
2 changes: 2 additions & 0 deletions grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func NewServer(sdkClients map[string]sdk.Client, metrics metrics.Reporter, conf
for _, c := range conf.Tls.Certificates {
if cert, err := tls.LoadX509KeyPair(c.Cert, c.Key); err == nil {
t.Certificates = append(t.Certificates, cert)
} else {
grpcLog.Errorf("failed to load the certificate and key pair: %s", err)
}
}
opts = append(opts, grpc.Creds(credentials.NewTLS(t)))
Expand Down
8 changes: 2 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/configcat/configcat-proxy/grpc"
"github.com/configcat/configcat-proxy/log"
"github.com/configcat/configcat-proxy/sdk"
"github.com/configcat/configcat-proxy/sdk/statistics"
"github.com/configcat/configcat-proxy/web"
"os"
"os/signal"
Expand Down Expand Up @@ -47,7 +46,7 @@ func run(closeSignal chan os.Signal) int {
errorChan := make(chan error)

// in the future we might implement an evaluation statistics reporter
var evalReporter statistics.Reporter
// var evalReporter statistics.Reporter

statusReporter := status.NewReporter(&conf)

Expand All @@ -66,7 +65,7 @@ func run(closeSignal chan os.Signal) int {
for key, sdkConf := range conf.SDKs {
sdkClients[key] = sdk.NewClient(&sdk.Context{
SDKConf: sdkConf,
EvalReporter: evalReporter,
EvalReporter: nil,
MetricsReporter: metricsReporter,
StatusReporter: statusReporter,
ProxyConf: &conf.HttpProxy,
Expand All @@ -89,9 +88,6 @@ func run(closeSignal chan os.Signal) int {
for {
select {
case <-closeSignal:
if evalReporter != nil {
evalReporter.Close()
}
for _, sdkClient := range sdkClients {
sdkClient.Close()
}
Expand Down
61 changes: 58 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,72 @@ import (
func TestAppMain(t *testing.T) {
t.Setenv("CONFIGCAT_SDKS", `{"sdk1":"XxPbCKmzIUGORk4vsufpzw/iC_KABprDEueeQs3yovVnQ"}`)

var code int
var exitCode int
closeSignal := make(chan os.Signal, 1)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
code = run(closeSignal)
exitCode = run(closeSignal)
wg.Done()
}()
time.Sleep(5 * time.Second)
closeSignal <- syscall.SIGTERM
wg.Wait()

assert.Equal(t, 0, code)
assert.Equal(t, 0, exitCode)
}

func TestAppMain_Invalid_Conf(t *testing.T) {
var exitCode int
closeSignal := make(chan os.Signal, 1)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
exitCode = run(closeSignal)
wg.Done()
}()
time.Sleep(1 * time.Second)
closeSignal <- syscall.SIGTERM
wg.Wait()

assert.Equal(t, 1, exitCode)
}

func TestAppMain_Invalid_Config_YAML(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = []string{"app", "-c=/tmp/non-existing.yml"}

var exitCode int
closeSignal := make(chan os.Signal, 1)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
exitCode = run(closeSignal)
wg.Done()
}()
time.Sleep(1 * time.Second)
closeSignal <- syscall.SIGTERM
wg.Wait()

assert.Equal(t, 1, exitCode)
}

func TestAppMain_ErrorChannel(t *testing.T) {
t.Setenv("CONFIGCAT_SDKS", `{"sdk1":"XxPbCKmzIUGORk4vsufpzw/iC_KABprDEueeQs3yovVnQ"}`)
t.Setenv("CONFIGCAT_TLS_ENABLED", "true")
t.Setenv("CONFIGCAT_TLS_CERTIFICATES", `[{"key":"./key","cert":"./cert"}]`)
var exitCode int
closeSignal := make(chan os.Signal, 1)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
exitCode = run(closeSignal)
wg.Done()
}()
time.Sleep(1 * time.Second)
closeSignal <- syscall.SIGTERM
wg.Wait()

assert.Equal(t, 1, exitCode)
}
2 changes: 2 additions & 0 deletions web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func NewServer(handler http.Handler, log log.Logger, conf *config.Config, errorC
for _, c := range conf.Tls.Certificates {
if cert, err := tls.LoadX509KeyPair(c.Cert, c.Key); err == nil {
t.Certificates = append(t.Certificates, cert)
} else {
httpLog.Errorf("failed to load the certificate and key pair: %s", err)
}
}
httpServer.TLSConfig = t
Expand Down

0 comments on commit 187e4b6

Please sign in to comment.