Skip to content

Commit

Permalink
👷 Add network type constant
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyonlin committed Feb 8, 2021
1 parent 0ec0f50 commit 00dbdd5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app.go
Expand Up @@ -284,7 +284,7 @@ type Config struct {
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)

This comment has been minimized.

Copy link
@suntong

suntong Feb 8, 2021

yeah, much better this time.

Only thing left, seems to me, are these two comment lines.

This comment has been minimized.

Copy link
@kiyonlin

kiyonlin Feb 8, 2021

Author Contributor

I think those hardcode are fine

// WARNING: When prefork is set to true, only "tcp4" and "tcp6" can be chose.
//
// Default: "tcp4"
// Default: NetworkTCP4
Network string
}

Expand Down Expand Up @@ -403,7 +403,7 @@ func New(config ...Config) *App {
app.config.JSONEncoder = json.Marshal
}
if app.config.Network == "" {
app.config.Network = "tcp4"
app.config.Network = NetworkTCP4
}

// Init app
Expand Down
6 changes: 3 additions & 3 deletions app_test.go
Expand Up @@ -392,7 +392,7 @@ func Test_App_Listener_TLS(t *testing.T) {
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}

ln, err := net.Listen("tcp4", ":3078")
ln, err := net.Listen(NetworkTCP4, ":3078")
utils.AssertEqual(t, nil, err)

ln = tls.NewListener(ln, config)
Expand Down Expand Up @@ -1151,7 +1151,7 @@ func Test_App_ReadTimeout(t *testing.T) {
go func() {
time.Sleep(500 * time.Millisecond)

conn, err := net.Dial("tcp4", "127.0.0.1:4004")
conn, err := net.Dial(NetworkTCP4, "127.0.0.1:4004")
utils.AssertEqual(t, nil, err)
defer conn.Close()

Expand Down Expand Up @@ -1183,7 +1183,7 @@ func Test_App_BadRequest(t *testing.T) {

go func() {
time.Sleep(500 * time.Millisecond)
conn, err := net.Dial("tcp4", "127.0.0.1:4005")
conn, err := net.Dial(NetworkTCP4, "127.0.0.1:4005")
utils.AssertEqual(t, nil, err)
defer conn.Close()

Expand Down
7 changes: 7 additions & 0 deletions helpers.go
Expand Up @@ -670,3 +670,10 @@ const (
HeaderXRobotsTag = "X-Robots-Tag"
HeaderXUACompatible = "X-UA-Compatible"
)

// Network types that are commonly used
const (
NetworkTCP = "tcp"
NetworkTCP4 = "tcp4"
NetworkTCP6 = "tcp6"
)
12 changes: 6 additions & 6 deletions helpers_test.go
Expand Up @@ -262,23 +262,23 @@ func Benchmark_Utils_IsNoCache(b *testing.B) {

func Test_Utils_lnMetadata(t *testing.T) {
t.Run("closed listen", func(t *testing.T) {
ln, err := net.Listen("tcp", ":0")
ln, err := net.Listen(NetworkTCP, ":0")
utils.AssertEqual(t, nil, err)

utils.AssertEqual(t, nil, ln.Close())

addr, config := lnMetadata("tcp", ln)
addr, config := lnMetadata(NetworkTCP, ln)

utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config == nil)
})

t.Run("non tls", func(t *testing.T) {
ln, err := net.Listen("tcp", ":0")
ln, err := net.Listen(NetworkTCP, ":0")

utils.AssertEqual(t, nil, err)

addr, config := lnMetadata("tcp4", ln)
addr, config := lnMetadata(NetworkTCP4, ln)

utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config == nil)
Expand All @@ -290,12 +290,12 @@ func Test_Utils_lnMetadata(t *testing.T) {

config := &tls.Config{Certificates: []tls.Certificate{cer}}

ln, err := net.Listen("tcp4", ":0")
ln, err := net.Listen(NetworkTCP4, ":0")
utils.AssertEqual(t, nil, err)

ln = tls.NewListener(ln, config)

addr, config := lnMetadata("tcp4", ln)
addr, config := lnMetadata(NetworkTCP4, ln)

utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config != nil)
Expand Down
10 changes: 5 additions & 5 deletions prefork_test.go
Expand Up @@ -23,15 +23,15 @@ func Test_App_Prefork_Child_Process(t *testing.T) {

app := New()

err := app.prefork("tcp4", "invalid", nil)
err := app.prefork(NetworkTCP4, "invalid", nil)
utils.AssertEqual(t, false, err == nil)

go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()

utils.AssertEqual(t, nil, app.prefork("tcp6", "[::]:", nil))
utils.AssertEqual(t, nil, app.prefork(NetworkTCP6, "[::]:", nil))

// Create tls certificate
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
Expand All @@ -45,7 +45,7 @@ func Test_App_Prefork_Child_Process(t *testing.T) {
utils.AssertEqual(t, nil, app.Shutdown())
}()

utils.AssertEqual(t, nil, app.prefork("tcp4", "127.0.0.1:", config))
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, "127.0.0.1:", config))
}

func Test_App_Prefork_Master_Process(t *testing.T) {
Expand All @@ -59,11 +59,11 @@ func Test_App_Prefork_Master_Process(t *testing.T) {
utils.AssertEqual(t, nil, app.Shutdown())
}()

utils.AssertEqual(t, nil, app.prefork("tcp4", ":3000", nil))
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, ":3000", nil))

dummyChildCmd = "invalid"

err := app.prefork("tcp4", "127.0.0.1:", nil)
err := app.prefork(NetworkTCP4, "127.0.0.1:", nil)
utils.AssertEqual(t, false, err == nil)
}

Expand Down

0 comments on commit 00dbdd5

Please sign in to comment.