micro--go-micro
3e885308a0
Fixes #2988. Brings 'golangci-lint run ./...' to zero issues (was ~373): - errcheck: explicitly ignore fire-and-forget calls with '_ =' (and a small errcheck.exclude-functions list for response writes — json Encoder.Encode, http ResponseWriter.Write, fmt.Fprint*); genuine cases handled. - unused: remove dead code (unexported decls and dead test helpers) and the imports they orphaned. - staticcheck: ST1005 error strings, ST1016 receiver names, S1000/S1017/S1019/ S1023 simplifications, SA4004/SA4006/SA4010 dead code, SA1021 net.IP.Equal, SA6002 (store *[]byte in sync.Pool). - govet: fix a context leak (lostcancel) in internal/util/mdns and move t.Fatal/Fatalf out of goroutines (testinggoroutine) in tests. - ineffassign, unconvert: mechanical fixes. CI: the Lint workflow now runs a blocking full-tree 'golangci-lint run' on pushes and PRs (dropped only-new-issues now that the tree is clean). Verified: go build, go vet, test compilation, and unit tests for the behaviourally-touched packages all pass. Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL Co-authored-by: Claude <noreply@anthropic.com>
92 行
1.7 KiB
Go
92 行
1.7 KiB
Go
package transport
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestMemoryTransport(t *testing.T) {
|
|
tr := NewMemoryTransport()
|
|
|
|
// bind / listen
|
|
l, err := tr.Listen("127.0.0.1:8080")
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
}
|
|
defer l.Close()
|
|
|
|
// accept
|
|
go func() {
|
|
if err := l.Accept(func(sock Socket) {
|
|
for {
|
|
var m Message
|
|
if err := sock.Recv(&m); err != nil {
|
|
return
|
|
}
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
t.Logf("Server Received %s", string(m.Body))
|
|
}
|
|
if err := sock.Send(&Message{
|
|
Body: []byte(`pong`),
|
|
}); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}); err != nil {
|
|
t.Errorf("Unexpected error accepting %v", err)
|
|
}
|
|
}()
|
|
|
|
// dial
|
|
c, err := tr.Dial("127.0.0.1:8080")
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error dialing %v", err)
|
|
}
|
|
defer c.Close()
|
|
|
|
// send <=> receive
|
|
for i := 0; i < 3; i++ {
|
|
if err := c.Send(&Message{
|
|
Body: []byte(`ping`),
|
|
}); err != nil {
|
|
return
|
|
}
|
|
var m Message
|
|
if err := c.Recv(&m); err != nil {
|
|
return
|
|
}
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
t.Logf("Client Received %s", string(m.Body))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestListener(t *testing.T) {
|
|
tr := NewMemoryTransport()
|
|
|
|
// bind / listen on random port
|
|
l, err := tr.Listen(":0")
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
}
|
|
defer l.Close()
|
|
|
|
// try again
|
|
l2, err := tr.Listen(":0")
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
}
|
|
defer l2.Close()
|
|
|
|
// now make sure it still fails
|
|
l3, err := tr.Listen(":8080")
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
}
|
|
defer l3.Close()
|
|
|
|
if _, err := tr.Listen(":8080"); err == nil {
|
|
t.Fatal("Expected error binding to :8080 got nil")
|
|
}
|
|
}
|