Skip to content

Commit

Permalink
ci: leverage reviewdog as automated code review tool
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Apr 8, 2020
1 parent d8a2ee6 commit 5db42f9
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 86 deletions.
46 changes: 26 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
language: go

env:
global:
- GO111MODULE=on

- secure: IZaEAuG1caQRBXf68hJDWNEveWc+Zk/Sf887DvMkpfWxCM43JqNPF6w0NEfFIyhtOeiVUPRiayn8Z8CjNr5xA/xvK4n31j8G5adYdOB0bR6pM0RoJ7p/Ji0Jni+8R5+IeDO+hORwA215TXPpjjmKeLgMdCXFZqZ1mlOLjBJlBu6sqZzfJe1k873fWJD+xGj5h9GdQveiWbEBtewUWoQeajtq+XHMzjwZxZwhOGUiQhkO9GIYih+oBQlggTf6VzT6FSN1/OTShC2yC6wtsJmqbD4GgGX2Ig8h9n9s/ZMJOFBpWdZMMIL6KJ4cWgANoH5AiBqjXglns8KB8iu7vh9zP4YckSO/s3ufb/w/VnqhZ8F5lWiD4pNyDdWxPKFLT5eO1aTlGmJYvHezZNVx3D6ASRryoH7pI9cduPbcLuA7+d3+FZ7lO/a30TRBHyW6vcDx7vjbTe1M462WGrHKNA6pzpWo3W5rjefo4Msg/wsh6+aD0XmIZ41lxXf/W/IpZ5RyR7R9q3sszdKxG+bkwIgNu0dUwMuenupsP9D+MBVURSY5Jgi1S6xz6R/MZnpN/wlLbI9t4FOcmkd5qj31XB8PDbcH4S+u27+iys9y7rdHwAbhF5qeGNeBwaAQaaXO/AAplJntXsph/7vPCTyHvQrH/shUBG6I2VkQpOJEmQyc1rE=
dist: bionic

os:
- linux
- osx
- windows

- linux
- osx
- windows
go:
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x
# - master

- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x
before_install:
- if [[ "${GO111MODULE}" = "on" ]]; then mkdir "${HOME}/go"; export GOPATH="${HOME}/go"; fi

- if [[ "${GO111MODULE}" = "on" ]]; then mkdir "${HOME}/go"; export GOPATH="${HOME}/go";
export PATH="$GOPATH/bin:$PATH"; fi
install:
- go get -u golang.org/x/lint/golint
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
| sh -s -- -b $(go env GOPATH)/bin v1.24.0
- mkdir -p ~/bin/ && export PATH="~/bin/:$PATH"
- curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh|
sh -s -- -b ~/bin
go_import_path: github.com/panjf2000/gnet

script:
- go test -v ./...
- go test -v -coverprofile=coverage.txt -covermode=count -timeout 60s

- golint ./... | reviewdog -f=golint -reporter=github-pr-review
- golangci-lint run --out-format=line-number -E golint -E misspell -E lll -E gofmt
| reviewdog -f=golangci-lint -reporter=github-pr-review
- go test -v ./...
- go test -v -coverprofile=coverage.txt -covermode=count -timeout 60s
after_success:
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then curl -s https://codecov.io/bash > .codecov && chmod +x .codecov && ./.codecov; else bash <(curl -s https://codecov.io/bash); fi
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then curl -s https://codecov.io/bash >
.codecov && chmod +x .codecov && ./.codecov; else bash <(curl -s https://codecov.io/bash);
fi
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<a title="Require Go Version" target="_blank" href="https://github.com/panjf2000/gnet"><img src="https://img.shields.io/badge/go-%3E%3D1.9-30dff3?style=flat-square&logo=go"></a>
<a title="Release" target="_blank" href="https://github.com/panjf2000/gnet/releases"><img src="https://img.shields.io/github/v/release/panjf2000/gnet.svg?color=161823&style=flat-square&logo=smartthings"></a>
<br/>
<a title="" target="_blank" href="https://golangci.com/r/github.com/panjf2000/gnet"><img src="https://golangci.com/badges/github.com/panjf2000/gnet.svg"></a>
<a title="Go Report Card" target="_blank" href="https://goreportcard.com/report/github.com/panjf2000/gnet"><img src="https://goreportcard.com/badge/github.com/panjf2000/gnet?style=flat-square"></a>
<a title="Doc for gnet" target="_blank" href="https://pkg.go.dev/github.com/panjf2000/gnet?tab=doc"><img src="https://img.shields.io/badge/go.dev-doc-007d9c?style=flat-square&logo=read-the-docs"></a>
<a title="gnet on Sourcegraph" target="_blank" href="https://sourcegraph.com/github.com/panjf2000/gnet?badge"><img src="https://sourcegraph.com/github.com/panjf2000/gnet/-/badge.svg?style=flat-square"></a>
Expand Down
1 change: 0 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<a title="Require Go Version" target="_blank" href="https://github.com/panjf2000/gnet"><img src="https://img.shields.io/badge/go-%3E%3D1.9-30dff3?style=flat-square&logo=go"></a>
<a title="Release" target="_blank" href="https://github.com/panjf2000/gnet/releases"><img src="https://img.shields.io/github/v/release/panjf2000/gnet.svg?color=161823&style=flat-square&logo=smartthings"></a>
<br/>
<a title="" target="_blank" href="https://golangci.com/r/github.com/panjf2000/gnet"><img src="https://golangci.com/badges/github.com/panjf2000/gnet.svg"></a>
<a title="Go Report Card" target="_blank" href="https://goreportcard.com/report/github.com/panjf2000/gnet"><img src="https://goreportcard.com/badge/github.com/panjf2000/gnet?style=flat-square"></a>
<a title="Doc for gnet" target="_blank" href="https://pkg.go.dev/github.com/panjf2000/gnet?tab=doc"><img src="https://img.shields.io/badge/go.dev-doc-007d9c?style=flat-square&logo=read-the-docs"></a>
<a title="gnet on Sourcegraph" target="_blank" href="https://sourcegraph.com/github.com/panjf2000/gnet?badge"><img src="https://sourcegraph.com/github.com/panjf2000/gnet/-/badge.svg?style=flat-square"></a>
Expand Down
6 changes: 4 additions & 2 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type (
frameLength int
}

// LengthFieldBasedFrameCodec is the refactoring from https://github.com/smallnest/goframe/blob/master/length_field_based_frameconn.go, licensed by Apache License 2.0.
// LengthFieldBasedFrameCodec is the refactoring from
// https://github.com/smallnest/goframe/blob/master/length_field_based_frameconn.go, licensed by Apache License 2.0.
// It encodes/decodes frames into/from TCP stream with value of the length field in the message.
// Original implementation: https://github.com/netty/netty/blob/4.1/codec/src/main/java/io/netty/handler/codec/LengthFieldBasedFrameDecoder.java
LengthFieldBasedFrameCodec struct {
Expand Down Expand Up @@ -137,7 +138,8 @@ type EncoderConfig struct {
LengthFieldLength int
// LengthAdjustment is the compensation value to add to the value of the length field
LengthAdjustment int
// LengthIncludesLengthFieldLength is true, the length of the prepended length field is added to the value of the prepended length field
// LengthIncludesLengthFieldLength is true, the length of the prepended length field is added to the value of
// the prepended length field
LengthIncludesLengthFieldLength bool
}

Expand Down
38 changes: 0 additions & 38 deletions doc.go

This file was deleted.

3 changes: 2 additions & 1 deletion gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ type (
}

// EventServer is a built-in implementation of EventHandler which sets up each method with a default implementation,
// you can compose it with your own implementation of EventHandler when you don't want to implement all methods in EventHandler.
// you can compose it with your own implementation of EventHandler when you don't want to implement all methods
// in EventHandler.
EventServer struct {
}
)
Expand Down
15 changes: 9 additions & 6 deletions gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (s *testCodecServer) React(frame []byte, c Conn) (out []byte, action Action
if frame != nil {
data := append([]byte{}, frame...)
_ = s.workerPool.Submit(func() {
c.AsyncWrite(data)
_ = c.AsyncWrite(data)
})
}
return
Expand Down Expand Up @@ -234,7 +234,8 @@ func testCodecServe(network, addr string, multicore, async bool, nclients int, r
if n > 4 {
n = 0
}
ts := &testCodecServer{network: network, addr: addr, multicore: multicore, async: async, nclients: nclients, codec: codec, workerPool: goroutine.Default()}
ts := &testCodecServer{network: network, addr: addr, multicore: multicore, async: async, nclients: nclients,
codec: codec, workerPool: goroutine.Default()}
if reuseport {
err = Serve(ts, network+"://"+addr, WithMulticore(multicore), WithTicker(true),
WithTCPKeepAlive(time.Minute*5), WithCodec(codec), WithReusePort(true))
Expand Down Expand Up @@ -277,7 +278,8 @@ func startCodecClient(network, addr string, multicore, async bool, codec ICodec)
panic(err)
}
if string(encodedData) != string(data2) && !async {
//panic(fmt.Sprintf("mismatch %s/multi-core:%t: %d vs %d bytes, %s:%s", network, multicore, len(encodedData), len(data2), string(encodedData), string(data2)))
//panic(fmt.Sprintf("mismatch %s/multi-core:%t: %d vs %d bytes, %s:%s", network, multicore,
// len(encodedData), len(data2), string(encodedData), string(data2)))
panic(fmt.Sprintf("mismatch %s/multi-core:%t: %d vs %d bytes", network, multicore, len(encodedData), len(data2)))
}
}
Expand Down Expand Up @@ -457,14 +459,14 @@ func (s *testServer) React(frame []byte, c Conn) (out []byte, action Action) {
c.ShiftN(1)
_ = s.workerPool.Submit(
func() {
c.AsyncWrite(buf.Bytes())
_ = c.AsyncWrite(buf.Bytes())
})
return
}
if s.network == "udp" {
_ = s.workerPool.Submit(
func() {
c.SendTo(frame)
_ = c.SendTo(frame)
})
return
}
Expand Down Expand Up @@ -643,7 +645,8 @@ func (t *testWakeConnServer) Tick() (delay time.Duration, action Action) {

func testWakeConn(network, addr string) {
svr := &testWakeConnServer{network: network, addr: addr}
must(Serve(svr, network+"://"+addr, WithTicker(true), WithNumEventLoop(2*runtime.NumCPU()), WithLogger(log.New(os.Stderr, "", log.LstdFlags))))
must(Serve(svr, network+"://"+addr, WithTicker(true), WithNumEventLoop(2*runtime.NumCPU()),
WithLogger(log.New(os.Stderr, "", log.LstdFlags))))
}

func TestShutdown(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type Options struct {
// ICodec encodes and decodes TCP stream.
Codec ICodec

// Logger is the customized logger for logging info, if it is not set, default standard logger from log package is used.
// Logger is the customized logger for logging info, if it is not set,
// default standard logger from log package is used.
Logger Logger
}

Expand Down
21 changes: 14 additions & 7 deletions ringbuffer/ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,17 @@ func (r *RingBuffer) Shift(n int) {
}
}

// Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered.
// Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error
// encountered.
// Even if Read returns n < len(p), it may use all of p as scratch space during the call.
// If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.
// If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting
// for more.
// When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes,
// it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call.
// it returns the number of bytes read. It may return the (non-nil) error from the same call or return the
// error (and n == 0) from a subsequent call.
// Callers should always process the n > 0 bytes returned before considering the error err.
// Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.
// Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF
// behaviors.
func (r *RingBuffer) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
Expand Down Expand Up @@ -180,8 +184,10 @@ func (r *RingBuffer) ReadByte() (b byte, err error) {
}

// Write writes len(p) bytes from p to the underlying buf.
// It returns the number of bytes written from p (n == len(p) > 0) and any error encountered that caused the write to stop early.
// If the length of p is greater than the writable capacity of this ring-buffer, it will allocate more memory to this ring-buffer.
// It returns the number of bytes written from p (n == len(p) > 0) and any error encountered that caused the write to
// stop early.
// If the length of p is greater than the writable capacity of this ring-buffer, it will allocate more memory to
// this ring-buffer.
// Write must not modify the slice data, even temporarily.
func (r *RingBuffer) Write(p []byte) (n int, err error) {
n = len(p)
Expand Down Expand Up @@ -311,7 +317,8 @@ func (r *RingBuffer) ByteBuffer() *bytebuffer.ByteBuffer {
return bb
}

// WithByteBuffer combines the available read bytes and the given bytes. It does not move the read pointer and only copy the available data.
// WithByteBuffer combines the available read bytes and the given bytes. It does not move the read pointer and
// only copy the available data.
func (r *RingBuffer) WithByteBuffer(b []byte) *bytebuffer.ByteBuffer {
if r.isEmpty {
return &bytebuffer.ByteBuffer{B: b}
Expand Down
19 changes: 10 additions & 9 deletions ringbuffer/ring_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestRingBuffer_Write(t *testing.T) {
t.Fatalf("expect free 48 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
}
buf := make([]byte, 5)
rb.Read(buf)
_, _ = rb.Read(buf)
if rb.Length() != 3 {
t.Fatalf("expect len 3 bytes but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
}
Expand All @@ -160,15 +160,16 @@ func TestRingBuffer_Write(t *testing.T) {
t.Fatalf("expect free 0 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
}
buf = make([]byte, 16)
rb.Read(buf)
_, _ = rb.Read(buf)
n, _ = rb.Write([]byte(strings.Repeat("1234", 4)))
if n != 16 {
t.Fatalf("expect write 16 bytes but got %d", n)
}
if rb.Free() != 0 {
t.Fatalf("expect free 0 bytes but got %d. r.w=%d, r.r=%d", rb.Free(), rb.w, rb.r)
}
if !bytes.Equal(append(buf, rb.ByteBuffer().Bytes()...), []byte(strings.Repeat("abcd", 32)+strings.Repeat("1234", 4))) {
if !bytes.Equal(append(buf, rb.ByteBuffer().Bytes()...),
[]byte(strings.Repeat("abcd", 32)+strings.Repeat("1234", 4))) {
t.Fatalf("expect 16 abcd and 4 1234 but got %s. r.w=%d, r.r=%d", rb.ByteBuffer().Bytes(), rb.w, rb.r)
}
}
Expand All @@ -190,7 +191,7 @@ func TestZeroRingBuffer(t *testing.T) {
t.Fatal("expect free is 0")
}
buf := []byte(strings.Repeat("1234", 12))
rb.Write(buf)
_, _ = rb.Write(buf)
if !(rb.Len() == 64 && rb.Cap() == 64) {
t.Fatalf("expect rb.Len()=64 and rb.Cap=64, but got rb.Len()=%d and rb.Cap()=%d", rb.Len(), rb.Cap())
}
Expand Down Expand Up @@ -247,7 +248,7 @@ func TestRingBuffer_Read(t *testing.T) {
}

// write 16 bytes to read
rb.Write([]byte(strings.Repeat("abcd", 4)))
_, _ = rb.Write([]byte(strings.Repeat("abcd", 4)))
n, err = rb.Read(buf)
if err != nil {
t.Fatalf("read failed: %v", err)
Expand All @@ -266,7 +267,7 @@ func TestRingBuffer_Read(t *testing.T) {
}

// write long slice to read, it will scale from 64 to 128 bytes.
rb.Write([]byte(strings.Repeat("abcd", 20)))
_, _ = rb.Write([]byte(strings.Repeat("abcd", 20)))
n, err = rb.Read(buf)
if err != nil {
t.Fatalf("read failed: %v", err)
Expand All @@ -285,7 +286,7 @@ func TestRingBuffer_Read(t *testing.T) {
}

rb.Reset()
rb.Write([]byte(strings.Repeat("1234", 32)))
_, _ = rb.Write([]byte(strings.Repeat("1234", 32)))
if !rb.IsFull() {
t.Fatal("ring buffer should be full")
}
Expand All @@ -309,7 +310,7 @@ func TestRingBuffer_Read(t *testing.T) {
if rb.r != 64 {
t.Fatalf("expect r.r=64 but got %d", rb.r)
}
rb.Write([]byte(strings.Repeat("1234", 4)))
_, _ = rb.Write([]byte(strings.Repeat("1234", 4)))
if rb.w != 16 {
t.Fatalf("expect r.w=16 but got %d", rb.w)
}
Expand Down Expand Up @@ -441,7 +442,7 @@ func TestRingBuffer_ByteInterface(t *testing.T) {
}

// read three
b, _ = rb.ReadByte()
_, _ = rb.ReadByte()
if rb.Length() != 0 {
t.Fatalf("expect len 0 byte but got %d. r.w=%d, r.r=%d", rb.Length(), rb.w, rb.r)
}
Expand Down

0 comments on commit 5db42f9

Please sign in to comment.