Skip to content

Commit

Permalink
Merge pull request #12 from pawelgaczynski/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
pawelgaczynski authored May 14, 2023
2 parents 92ec8ee + cc6c176 commit cc55fb7
Show file tree
Hide file tree
Showing 17 changed files with 253 additions and 63 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Gain package

on:
push:
branches:
- main
- dev
paths-ignore:
- '**.md'
pull_request:
branches:
- main
- dev
paths-ignore:
- '**.md'

# env:
# TEST_PRINT_MAPS_AND_FDS: true
# TEST_LOGGER_LEVEL: trace

jobs:
build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Tune GitHub-hosted runner network
uses: smorimoto/tune-github-hosted-runner-network@v1

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.19.x'

- name: Install dependencies
run: go get .

- name: Run coverage
run: go test -v ./... -timeout 30s -coverprofile=coverage.out -covermode=atomic

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<a name="readme-top"></a>

[![Apache 2.0 License][license-shield]][license-url]

<br />
<div align="center">
<a href="https://github.com/pawelgaczynski/gain">
<img src="images/gain.png" alt="Logo" width="495" height="327">
</a>

[![Apache 2.0 License][license-shield]][license-url]
[![Go Reference](https://pkg.go.dev/badge/github.com/pawelgaczynski/gain.svg)](https://pkg.go.dev/github.com/pawelgaczynski/gain)
[![Go Report Card](https://goreportcard.com/badge/github.com/pawelgaczynski/gain)](https://goreportcard.com/report/github.com/pawelgaczynski/gain)

<h2 align="center">Gain</h2>

<p align="center">
Expand Down Expand Up @@ -55,6 +57,7 @@ Gain is a high-performance networking framework written entirely in Go. It uses
Articles about the project:

* [Medium - Meet Gain - the New Fastest Go TCP Framework](https://medium.com/better-programming/gain-the-new-fastest-go-tcp-framework-40ec111d40e6)
* [Medium - Writing High-performance TCP Applications Using the Gain Web Framework](https://medium.com/better-programming/an-introduction-to-gain-part-1-writing-high-performance-tcp-application-df5f7253e54a)

<br/>

Expand All @@ -67,15 +70,15 @@ Articles about the project:

### Prerequisites

Gain requires Go 1.18+
Gain requires Go 1.19+

<br/>

### Installation

1. Install the framework
```sh
go get -u github.com/pawelgaczynski/gain@v0.2.0-alpha
go get -u github.com/pawelgaczynski/gain@v0.3.0-alpha
```

<p align="right">(<a href="#readme-top">back to top</a>)</p>
Expand Down
11 changes: 5 additions & 6 deletions common_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"time"

"github.com/pawelgaczynski/gain"
"github.com/rs/zerolog"
. "github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -121,7 +120,7 @@ type testConnClient struct {
}

func (c *testConnClient) Dial() {
conn, err := net.DialTimeout(c.network, fmt.Sprintf("localhost:%d", c.port), time.Millisecond*500)
conn, err := net.DialTimeout(c.network, fmt.Sprintf("127.0.0.1:%d", c.port), time.Second)
Nil(c.t, err)
NotNil(c.t, conn)
c.conn = conn
Expand Down Expand Up @@ -212,7 +211,7 @@ func newTestConnServer(
) (gain.Server, int) {
t.Helper()
opts := []gain.ConfigOption{
gain.WithLoggerLevel(zerolog.FatalLevel),
gain.WithLoggerLevel(getTestLoggerLevel()),
gain.WithWorkers(4),
gain.WithArchitecture(architecture),
gain.WithAsyncHandler(async),
Expand All @@ -224,7 +223,7 @@ func newTestConnServer(
testPort := getTestPort()

go func() {
err := server.Start(fmt.Sprintf("%s://localhost:%d", network, testPort))
err := server.Start(fmt.Sprintf("%s://127.0.0.1:%d", network, testPort))
if err != nil {
log.Panic(err)
}
Expand Down Expand Up @@ -252,7 +251,7 @@ func testConnAddress(
t.Helper()
numberOfClients := 10
opts := []gain.ConfigOption{
gain.WithLoggerLevel(zerolog.FatalLevel),
gain.WithLoggerLevel(getTestLoggerLevel()),
gain.WithWorkers(4),
gain.WithArchitecture(architecture),
}
Expand Down Expand Up @@ -305,7 +304,7 @@ func testConnAddress(
testPort := getTestPort()

go func() {
serverErr := server.Start(fmt.Sprintf("%s://localhost:%d", network, testPort))
serverErr := server.Start(fmt.Sprintf("%s://127.0.0.1:%d", network, testPort))
if err != nil {
log.Panic(serverErr)
}
Expand Down
22 changes: 14 additions & 8 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/pawelgaczynski/gain"
gainErrors "github.com/pawelgaczynski/gain/pkg/errors"
gainNet "github.com/pawelgaczynski/gain/pkg/net"
"github.com/rs/zerolog"
. "github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -192,7 +191,7 @@ var deafultAfterDial = func(t *testing.T, conn net.Conn, repeats, clientIndex in

func dialClient(t *testing.T, protocol string, port int, clientConnChan chan net.Conn) {
t.Helper()
conn, err := net.DialTimeout(protocol, fmt.Sprintf("localhost:%d", port), time.Second)
conn, err := net.DialTimeout(protocol, fmt.Sprintf("127.0.0.1:%d", port), time.Second)
Nil(t, err)
NotNil(t, conn)
clientConnChan <- conn
Expand All @@ -202,7 +201,7 @@ func dialClientRW(t *testing.T, protocol string, port int,
afterDial afterDialCallback, repeats, clientIndex int, clientConnChan chan net.Conn,
) {
t.Helper()
conn, err := net.DialTimeout(protocol, fmt.Sprintf("localhost:%d", port), 2*time.Second)
conn, err := net.DialTimeout(protocol, fmt.Sprintf("127.0.0.1:%d", port), 2*time.Second)
Nil(t, err)
NotNil(t, conn)
afterDial(t, conn, repeats, clientIndex)
Expand Down Expand Up @@ -233,7 +232,7 @@ func testServer(t *testing.T, testConfig testServerConfig, architecture gain.Ser
log.Panic("network protocol is missing")
}
opts := []gain.ConfigOption{
gain.WithLoggerLevel(zerolog.FatalLevel),
gain.WithLoggerLevel(getTestLoggerLevel()),
gain.WithAsyncHandler(testConfig.asyncHandler),
gain.WithGoroutinePool(testConfig.goroutinePool),
gain.WithCPUAffinity(testConfig.cpuAffinity),
Expand All @@ -254,7 +253,7 @@ func testServer(t *testing.T, testConfig testServerConfig, architecture gain.Ser
testPort := getTestPort()

go func() {
err := server.Start(fmt.Sprintf("%s://localhost:%d", testConfig.protocol, testPort))
err := server.Start(fmt.Sprintf("%s://127.0.0.1:%d", testConfig.protocol, testPort))
if err != nil {
log.Panic(err)
}
Expand Down Expand Up @@ -400,11 +399,11 @@ func testCloseServer(t *testing.T, network string, architecture gain.ServerArchi

_, err := rand.Read(data)
Nil(t, err)
clientsGroup.SetDeadline(time.Now().Add(time.Millisecond * 500))
clientsGroup.SetDeadline(time.Now().Add(time.Second))
clientsGroup.Write(data)
buffer := make([]byte, 512)

clientsGroup.SetDeadline(time.Now().Add(time.Millisecond * 500))
clientsGroup.SetDeadline(time.Now().Add(time.Second))
clientsGroup.Read(buffer)

clientsGroup.SetDeadline(time.Time{})
Expand Down Expand Up @@ -465,7 +464,7 @@ func testCloseConn(t *testing.T, async bool, architecture gain.ServerArchitectur
clientDoneWg.Add(1)

go func(wg *sync.WaitGroup) {
conn, cErr := net.DialTimeout(gainNet.TCP, fmt.Sprintf("localhost:%d", port), time.Second)
conn, cErr := net.DialTimeout(gainNet.TCP, fmt.Sprintf("127.0.0.1:%d", port), time.Second)
Nil(t, cErr)
NotNil(t, conn)
testData := []byte("testdata1234567890")
Expand Down Expand Up @@ -497,6 +496,13 @@ func testCloseConn(t *testing.T, async bool, architecture gain.ServerArchitectur
func testLargeRead(t *testing.T, network string, architecture gain.ServerArchitecture) {
t.Helper()

if !checkKernelCompatibility(5, 19) {
//nolint
fmt.Println("Not supported by kernel")

return
}

doublePageSize := os.Getpagesize() * 4
data := make([]byte, doublePageSize)
_, err := rand.Read(data)
Expand Down
77 changes: 50 additions & 27 deletions event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ package gain_test

import (
"crypto/rand"
"errors"
"fmt"
"log"
"net"
"sync"
"syscall"
"testing"
"time"

"github.com/pawelgaczynski/gain"
"github.com/pawelgaczynski/gain/pkg/errors"
gainErrors "github.com/pawelgaczynski/gain/pkg/errors"
gainNet "github.com/pawelgaczynski/gain/pkg/net"
. "github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -52,9 +54,9 @@ func testHandlerMethod(

server, port := newTestConnServer(t, network, asyncHandler, architecture, eventHandlerTester)

conn, err := net.DialTimeout(network, fmt.Sprintf("localhost:%d", port), time.Second*500)
if err != nil {
conn, err = net.DialTimeout(network, fmt.Sprintf("localhost:%d", port), time.Second*500)
conn, err := net.DialTimeout(network, fmt.Sprintf("127.0.0.1:%d", port), time.Second)
if err != nil && !errors.Is(err, syscall.ECONNRESET) {
conn, err = net.DialTimeout(network, fmt.Sprintf("127.0.0.1:%d", port), time.Second)
if err != nil {
log.Panic(err)
}
Expand All @@ -78,14 +80,14 @@ func testHandlerMethod(
eventHandlerTester.onCloseWg.Wait()
}

eventHandlerTester.finished.Store(true)

Equal(t, 1, int(eventHandlerTester.onStartCount.Load()))
Equal(t, callCounts[0], int(eventHandlerTester.onAcceptCount.Load()))
Equal(t, callCounts[1], int(eventHandlerTester.onReadCount.Load()))
Equal(t, callCounts[2], int(eventHandlerTester.onWriteCount.Load()))
Equal(t, callCounts[3], int(eventHandlerTester.onCloseCount.Load()))

eventHandlerTester.finished.Store(true)

server.Shutdown()
}

Expand Down Expand Up @@ -185,16 +187,31 @@ func TestEventHandlerOnRead(t *testing.T) {
},
}
clientBehavior := func(conn net.Conn) {
err := conn.SetWriteDeadline(time.Now().Add(time.Millisecond * 500))
if err != nil {
log.Panic(err)
}

n, err := conn.Write(eventHandlerTestData)
Equal(t, eventHandlerTestDataSize, n)
Nil(t, err)
buffer := make([]byte, 1024)

err = conn.SetReadDeadline(time.Now().Add(time.Millisecond * 500))
if err != nil {
log.Panic(err)
}
n, err = conn.Read(buffer)
Equal(t, n, 0)
NotNil(t, err)
conn.Close()
}

testCases := createTestCases("JustRead", both, callbacks, clientBehavior, [][]int{
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 1},
{1, 1, 0, 1},
{1, 1, 0, 1},
{1, 1, 0, 1},
{0, 1, 0, 0},
{0, 1, 0, 0},
})
Expand Down Expand Up @@ -225,13 +242,14 @@ func TestEventHandlerOnRead(t *testing.T) {
Equal(t, eventHandlerTestDataSize, n)
Nil(t, err)
Equal(t, eventHandlerTestData, buffer[:eventHandlerTestDataSize])
conn.Close()
}

testCases = createTestCases("ReadAndWrite", both, callbacks, clientBehavior, [][]int{
{1, 1, 1, 0},
{1, 1, 1, 0},
{1, 1, 1, 0},
{1, 1, 1, 0},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{0, 1, 1, 0},
{0, 1, 1, 0},
})
Expand All @@ -252,7 +270,7 @@ func TestEventHandlerOnRead(t *testing.T) {
onWriteCallback: func(conn gain.Conn, n int) {
buf, err := conn.Next(-1)
Equal(t, 0, len(buf))
Equal(t, errors.ErrConnectionClosed, err)
Equal(t, gainErrors.ErrConnectionClosed, err)
},
}
clientBehavior = func(conn net.Conn) {
Expand Down Expand Up @@ -286,10 +304,12 @@ func TestEventHandlerOnAccept(t *testing.T) {
},
}
clientBehavior := func(conn net.Conn) {
time.Sleep(time.Millisecond * 50)
n, err := conn.Write(eventHandlerTestData)
Equal(t, 0, n)
NotNil(t, err)
if conn != nil {
time.Sleep(time.Millisecond * 50)
n, err := conn.Write(eventHandlerTestData)
Equal(t, 0, n)
NotNil(t, err)
}
}

testCases := createTestCases("JustClose", tcp, callbacks, clientBehavior, [][]int{
Expand Down Expand Up @@ -350,13 +370,14 @@ func TestEventHandlerOnAccept(t *testing.T) {
Equal(t, eventHandlerTestDataSize, n)
Nil(t, err)
Equal(t, eventHandlerTestData, buffer[:eventHandlerTestDataSize])
conn.Close()
}

testCases = createTestCases("Write", tcp, callbacks, clientBehavior, [][]int{
{1, 0, 1, 0},
{1, 0, 1, 0},
{1, 0, 1, 0},
{1, 0, 1, 0},
{1, 0, 1, 1},
{1, 0, 1, 1},
{1, 0, 1, 1},
{1, 0, 1, 1},
})

testEventHandler(t, testCases)
Expand Down Expand Up @@ -428,13 +449,15 @@ func TestEventHandlerOnWrite(t *testing.T) {
Nil(t, err)
Equal(t, eventHandlerTestData, buffer[:eventHandlerTestDataSize])
}

conn.Close()
}

testCases := createTestCases("AdditionalWrite", tcp, callbacks, clientBehavior, [][]int{
{1, 1, 2, 0},
{1, 1, 2, 0},
{1, 1, 2, 0},
{1, 1, 2, 0},
{1, 1, 2, 1},
{1, 1, 2, 1},
{1, 1, 2, 1},
{1, 1, 2, 1},
})

testEventHandler(t, testCases)
Expand Down
Loading

0 comments on commit cc55fb7

Please sign in to comment.