From 9e921de36cb10449615adea85e60e32c59566d05 Mon Sep 17 00:00:00 2001 From: realDragonium Date: Fri, 9 Jul 2021 20:06:55 +0200 Subject: [PATCH] implemented logger in worker tests, race condition in workers only seems to be there when VSCode is running the test with cover mode --- config/file.go | 2 -- config/types.go | 4 ++++ proxy/listener.go | 8 ++++---- proxy/proxy.go | 5 +++++ proxy/worker_test.go | 47 +++++++++++++++++++++++++++++++------------- 5 files changed, 46 insertions(+), 20 deletions(-) diff --git a/config/file.go b/config/file.go index 409c6d0..aaf3b34 100644 --- a/config/file.go +++ b/config/file.go @@ -3,7 +3,6 @@ package config import ( "encoding/json" "io/ioutil" - "log" "os" "path/filepath" ) @@ -22,7 +21,6 @@ func ReadServerConfigs(path string) ([]ServerConfig, error) { return nil }) if err != nil { - log.Println(err) return cfgs, err } for _, filePath := range filePaths { diff --git a/config/types.go b/config/types.go index c749fa8..874069e 100644 --- a/config/types.go +++ b/config/types.go @@ -1,6 +1,8 @@ package config import ( + "io" + "github.com/realDragonium/Ultraviolet/mc" ) @@ -31,4 +33,6 @@ type UltravioletConfig struct { NumberOfWorkers int `json:"numberOfWorkers"` NumberOfConnWorkers int `json:"numberOfConnWorkers"` NumberOfStatusWorkers int `json:"numberOfStatusWorkers"` + + LogOutput io.Writer } diff --git a/proxy/listener.go b/proxy/listener.go index 2d9a59e..700e921 100644 --- a/proxy/listener.go +++ b/proxy/listener.go @@ -63,11 +63,11 @@ func ReadConnection(c net.Conn, reqCh chan McRequest) { // Add better error handling handshakePacket, err := conn.ReadPacket() if err != nil { - log.Printf("Error while reading handshake packet: %v", err) + // log.Printf("Error while reading handshake packet: %v", err) } handshake, err := mc.UnmarshalServerBoundHandshake(handshakePacket) if err != nil { - log.Printf("Error while unmarshaling handshake packet: %v", err) + // log.Printf("Error while unmarshaling handshake packet: %v", err) } if handshake.NextState != mc.HandshakeLoginState && handshake.NextState != mc.HandshakeStatusState { @@ -92,11 +92,11 @@ func ReadConnection(c net.Conn, reqCh chan McRequest) { // Add better error handling loginPacket, err = conn.ReadPacket() if err != nil { - log.Printf("Error while reading login start packet: %v", err) + // log.Printf("Error while reading login start packet: %v", err) } loginStart, err := mc.UnmarshalServerBoundLoginStart(loginPacket) if err != nil { - log.Printf("Error while unmarshaling login start packet: %v", err) + // log.Printf("Error while unmarshaling login start packet: %v", err) } req.Type = LOGIN req.Username = string(loginStart.Name) diff --git a/proxy/proxy.go b/proxy/proxy.go index acdf14d..3c86a41 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -1,6 +1,7 @@ package proxy import ( + "log" "sync" "time" @@ -44,6 +45,10 @@ func SetupWorkers(cfg config.UltravioletConfig, serverCfgs []config.ServerConfig connCh := make(chan ConnRequest) statusCh := make(chan StatusRequest) + if cfg.LogOutput != nil { + log.SetOutput(cfg.LogOutput) + } + defaultStatus := cfg.DefaultStatus.Marshal() workerServerCfgs := make(map[string]WorkerServerConfig) for _, serverCfg := range serverCfgs { diff --git a/proxy/worker_test.go b/proxy/worker_test.go index 32d6e65..3d8db11 100644 --- a/proxy/worker_test.go +++ b/proxy/worker_test.go @@ -4,6 +4,8 @@ import ( "bytes" "errors" "fmt" + "io" + "log" "net" "strings" "sync" @@ -60,6 +62,19 @@ func testAddr() string { return addr } +type testLogger struct { + t *testing.T +} + +func (tLog *testLogger) Write(b []byte) (n int, err error) { + tLog.t.Log(string(b)) + return 0, nil +} + +func assignTestLogger(t *testing.T) { + log.SetOutput(&testLogger{t: t}) +} + func unknownServerStatusPk() mc.Packet { return unknownServerStatus().Marshal() } @@ -85,8 +100,9 @@ func defaultOfflineStatus() mc.AnotherStatusResponse { } //Test Help methods -func setupBasicTestWorkers(serverCfgs ...config.ServerConfig) chan<- proxy.McRequest { - reqCh, proxyCh := setupTestWorkers(simpleUltravioletConfig(), serverCfgs...) +func setupBasicTestWorkers(t *testing.T, serverCfgs ...config.ServerConfig) chan<- proxy.McRequest { + logOutput := &testLogger{t: t} + reqCh, proxyCh := setupTestWorkers(simpleUltravioletConfig(logOutput), serverCfgs...) go func() { for { <-proxyCh @@ -102,12 +118,13 @@ func setupTestWorkers(cfg config.UltravioletConfig, serverCfgs ...config.ServerC return reqCh, proxyCh } -func simpleUltravioletConfig() config.UltravioletConfig { +func simpleUltravioletConfig(logOutput io.Writer) config.UltravioletConfig { return config.UltravioletConfig{ DefaultStatus: unknownServerStatus(), NumberOfWorkers: 1, NumberOfConnWorkers: 1, NumberOfStatusWorkers: 1, + LogOutput: logOutput, } } @@ -183,9 +200,8 @@ func createListener(t *testing.T, addr string) (<-chan net.Conn, <-chan error) { // Actual Tests func TestWorker_CanReceiveRequests(t *testing.T) { - serverCfg := config.ServerConfig{} - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) select { case reqCh <- proxy.McRequest{}: t.Log("worker has successfully received request") @@ -202,7 +218,7 @@ func TestUnknownAddr(t *testing.T) { Type: tc.reqType, ServerAddr: "some weird server address", } - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) answer := sendRequest_TestTimeout(t, reqCh, req) if answer.Action != tc.unknownAction { t.Errorf("expcted: %v \ngot: %v", tc.unknownAction, answer.Action) @@ -238,7 +254,7 @@ func TestKnownAddr_OfflineServer(t *testing.T) { ServerAddr: serverAddr, } offlineStatusPk := defaultOfflineStatusPacket() - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) answer := sendRequest_TestTimeout(t, reqCh, req) if answer.Action != tc.offlineAction { t.Errorf("expcted: %v \ngot: %v", tc.offlineAction, answer.Action) @@ -274,7 +290,7 @@ func TestKnownAddr_OnlineServer(t *testing.T) { ServerAddr: serverAddr, } createListener(t, targetAddr) - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) answer := sendRequest_TestTimeout(t, reqCh, req) if answer.Action != tc.onlineAction { t.Fatalf("expcted: %v \ngot: %v", tc.onlineAction, answer.Action) @@ -303,7 +319,7 @@ func TestProxyBind(t *testing.T) { ServerAddr: serverAddr, } connCh, errorCh := createListener(t, targetAddr) - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) sendRequest_IgnoreResult(reqCh, req) conn := <-connCh // State check call (proxy bind should be used here too) receivedBind := netAddrToIp(conn.RemoteAddr()) @@ -363,7 +379,7 @@ func TestProxyProtocol(t *testing.T) { } }() - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) sendRequest_IgnoreResult(reqCh, req) <-connCh // State check call (no proxy protocol in here) select { @@ -394,7 +410,7 @@ func TestProxy_ManyRequestsWillRateLimit(t *testing.T) { RateLimit: rateLimit, RateDuration: rateLimitDuration.String(), } - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) req := proxy.McRequest{ Type: tc.reqType, ServerAddr: serverAddr, @@ -424,7 +440,7 @@ func TestProxy_WillAllowNewConn_AfterDurationEnded(t *testing.T) { RateLimit: rateLimit, RateDuration: rateLimitDuration.String(), } - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) req := proxy.McRequest{ Type: tc.reqType, ServerAddr: serverAddr, @@ -454,7 +470,7 @@ func TestServerState_DoesntCallBeforeCooldownIsOver(t *testing.T) { ProxyTo: targetAddr, UpdateCooldown: updateCooldown.String(), } - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) req := proxy.McRequest{ Type: tc.reqType, ServerAddr: serverAddr, @@ -477,17 +493,20 @@ func TestServerState_ShouldCallAgainOutOfCooldown(t *testing.T) { t.Run(fmt.Sprintf("reqType-%v", tc.reqType), func(t *testing.T) { serverAddr := "ultraviolet" targetAddr := testAddr() + t.Log(targetAddr) updateCooldown := defaultChTimeout serverCfg := config.ServerConfig{ MainDomain: serverAddr, ProxyTo: targetAddr, UpdateCooldown: updateCooldown.String(), + DialTimeout: "1s", } - reqCh := setupBasicTestWorkers(serverCfg) + reqCh := setupBasicTestWorkers(t, serverCfg) req := proxy.McRequest{ Type: tc.reqType, ServerAddr: serverAddr, } + // sendRequest_IgnoreResult(reqCh, req) sendRequest_TestTimeout(t, reqCh, req) time.Sleep(longerChTimeout) connCh, _ := createListener(t, targetAddr)