Skip to content

Commit

Permalink
implemented logger in worker tests, race condition in workers only se…
Browse files Browse the repository at this point in the history
…ems to be there when VSCode is running the test with cover mode
  • Loading branch information
realDragonium committed Jul 9, 2021
1 parent e0442f9 commit 9e921de
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
2 changes: 0 additions & 2 deletions config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
)
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions config/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"io"

"github.com/realDragonium/Ultraviolet/mc"
)

Expand Down Expand Up @@ -31,4 +33,6 @@ type UltravioletConfig struct {
NumberOfWorkers int `json:"numberOfWorkers"`
NumberOfConnWorkers int `json:"numberOfConnWorkers"`
NumberOfStatusWorkers int `json:"numberOfStatusWorkers"`

LogOutput io.Writer
}
8 changes: 4 additions & 4 deletions proxy/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proxy

import (
"log"
"sync"
"time"

Expand Down Expand Up @@ -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 {
Expand Down
47 changes: 33 additions & 14 deletions proxy/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net"
"strings"
"sync"
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 9e921de

Please sign in to comment.