Skip to content

Commit 4c1558e

Browse files
committed
Add more tests
1 parent ac9e462 commit 4c1558e

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
.idea/
22
.vscode/
33
vendor/
4-
websocket_integration_test.go

CHANGELOG.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
55

66
## [Unreleased]
77

8+
## [v1.0.1] - 2020-11-14
9+
### Added
10+
- Added the ability to run the status command on a real Rust server. To do this, set environment variables `TEST_RUST_SERVER=true`,
11+
`TEST_RUST_SERVER_ADDR` and `TEST_RUST_SERVER_PASSWORD` with address and password from Rust remote console.
12+
- Added deadline test.
13+
14+
### Changed
15+
- Changed CI workflows and related badges. Integration with Travis-CI was changed to GitHub actions workflow. Golangci-lint
16+
job was joined with tests workflow.
17+
818
## [v1.0.0] - 2020-11-13
919
### Added
1020
- Added mockserver and tests.
@@ -14,5 +24,6 @@ All notable changes to this project will be documented in this file.
1424
### Added
1525
- Initial implementation.
1626

17-
[Unreleased]: https://github.com/gorcon/websocket/compare/v1.0.0...HEAD
27+
[Unreleased]: https://github.com/gorcon/websocket/compare/v1.0.1...HEAD
28+
[v1.0.1]: https://github.com/gorcon/websocket/compare/v1.0.0...v1.0.1
1829
[v1.0.0]: https://github.com/gorcon/websocket/compare/v0.1.0...v1.0.0

websocket_mockserver_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"net/http"
88
"strings"
9+
"time"
910

1011
"github.com/gorilla/websocket"
1112
)
@@ -62,6 +63,13 @@ func mockHandlers() http.Handler {
6263
Identifier: message.Identifier,
6364
Type: "Generic",
6465
}
66+
case "deadline":
67+
time.Sleep(DefaultDeadline + 1*time.Second)
68+
response = Message{
69+
Message: fmt.Sprintf("sleep for %d secends", DefaultDeadline+1*time.Second),
70+
Identifier: message.Identifier,
71+
Type: "Generic",
72+
}
6573
default:
6674
response = Message{
6775
Message: fmt.Sprintf("Command '%s' not found", message.Message),

websocket_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package websocket
33
import (
44
"fmt"
55
"net/http/httptest"
6+
"os"
67
"testing"
78
"time"
89

@@ -74,6 +75,20 @@ func TestConn_Execute(t *testing.T) {
7475
assert.Equal(t, 0, len(result))
7576
})
7677

78+
t.Run("read deadline", func(t *testing.T) {
79+
conn, err := Dial(server.Listener.Addr().String(), MockPassword, SetDeadline(1*time.Second))
80+
if !assert.NoError(t, err) {
81+
return
82+
}
83+
defer func() {
84+
assert.NoError(t, conn.Close())
85+
}()
86+
87+
result, err := conn.Execute("deadline")
88+
assert.EqualError(t, err, fmt.Sprintf("read tcp %s->%s: i/o timeout", conn.LocalAddr(), conn.RemoteAddr()))
89+
assert.Equal(t, 0, len(result))
90+
})
91+
7792
t.Run("unknown command", func(t *testing.T) {
7893
conn, err := Dial(server.Listener.Addr().String(), MockPassword)
7994
if !assert.NoError(t, err) {
@@ -101,4 +116,42 @@ func TestConn_Execute(t *testing.T) {
101116
assert.NoError(t, err)
102117
assert.Equal(t, MockCommandStatusResponseText, result)
103118
})
119+
120+
// Environment variable TEST_RUST_SERVER allows to sends commands to real
121+
// Rust server.
122+
//
123+
// Some Rust commands:
124+
// console.tail 5
125+
// status
126+
// playerlist
127+
// serverinfo
128+
if run := getVar("TEST_RUST_SERVER", "false"); run == "true" {
129+
addr := getVar("TEST_RUST_SERVER_ADDR", "127.0.0.1:28016")
130+
password := getVar("TEST_RUST_SERVER_PASSWORD", "docker")
131+
132+
t.Run("rust server", func(t *testing.T) {
133+
conn, err := Dial(addr, password)
134+
if err != nil {
135+
t.Fatal(err)
136+
}
137+
defer func() {
138+
assert.NoError(t, conn.Close())
139+
}()
140+
141+
result, err := conn.Execute("status")
142+
assert.NoError(t, err)
143+
assert.NotEmpty(t, result)
144+
145+
fmt.Println(result)
146+
})
147+
}
148+
}
149+
150+
// getVar returns environment variable or default value.
151+
func getVar(key string, fallback string) string {
152+
if value := os.Getenv(key); value != "" {
153+
return value
154+
}
155+
156+
return fallback
104157
}

0 commit comments

Comments
 (0)