Skip to content

Commit f63586e

Browse files
committed
code health: fix all places highlighted by linter
Changed error's suppression in check.yaml. The suppression of rule `errcheck` may be removed after fixing errors check in all methods with calling encodeXxx inside. See details below. The rules `structcheck` and `unused` highlight the 'hack' with using _msgpack struct{} `msgpack:",asArray"`. The workaround with `//nolint` is not the best way to suppress it, cause this comment gets rendered by godoc and there is no way to make it invisible. For details see golang/go#20925. Suppressed the highlighting of error check in all methods, having encodeXxx inside. For now these methods are not able to return any error cause of internal implementation of writer interface (see smallbuf.go). For future, if the implementation of writer will be changed, and there will be a need to check errors, we must think about how to say to compiler that the error check is 'unlikely'. Fixed the use of time package API, all places with calls of time.Now().Sub(). Now time package propose the explicit time.Until(). Replaced all calls of Errorf() with Fatalf() in tests. That change prevents nil dereferences below in the code and stops test function execution, where it is expected in tests. Suppressed the highlighting of all unused constants and functions (Rules structcheck,unused in golangci-lint). Fixed calling of Fatalf from non-testing goroutine in queue tests. It is not a valid way to stop test from another goroutine. Closes #142 Closes #150
1 parent e9b9ba1 commit f63586e

File tree

13 files changed

+113
-89
lines changed

13 files changed

+113
-89
lines changed

.github/workflows/check.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ jobs:
3939
- name: golangci-lint
4040
uses: golangci/golangci-lint-action@v2
4141
with:
42-
args: --issues-exit-code=0 -E gofmt
42+
args: -E gofmt --disable errcheck,unused,structcheck

connection.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func (conn *Connection) createConnection(reconnect bool) (err error) {
495495
conn.notify(ReconnectFailed)
496496
reconnects++
497497
conn.mutex.Unlock()
498-
time.Sleep(now.Add(conn.opts.Reconnect).Sub(time.Now()))
498+
time.Sleep(time.Until(now.Add(conn.opts.Reconnect)))
499499
conn.mutex.Lock()
500500
}
501501
if conn.state == connClosed {
@@ -688,7 +688,7 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
688688
*pair.last = fut
689689
pair.last = &fut.next
690690
if conn.opts.Timeout > 0 {
691-
fut.timeout = time.Now().Sub(epoch) + conn.opts.Timeout
691+
fut.timeout = time.Until(epoch) + conn.opts.Timeout
692692
}
693693
shard.rmut.Unlock()
694694
if conn.rlimit != nil && conn.opts.RLimitAction == RLimitWait {
@@ -796,9 +796,9 @@ func (conn *Connection) timeouts() {
796796
return
797797
case <-t.C:
798798
}
799-
minNext := time.Now().Sub(epoch) + timeout
799+
minNext := time.Until(epoch) + timeout
800800
for i := range conn.shard {
801-
nowepoch = time.Now().Sub(epoch)
801+
nowepoch = time.Until(epoch)
802802
shard := &conn.shard[i]
803803
for pos := range shard.requests {
804804
shard.rmut.Lock()
@@ -825,7 +825,7 @@ func (conn *Connection) timeouts() {
825825
shard.rmut.Unlock()
826826
}
827827
}
828-
nowepoch = time.Now().Sub(epoch)
828+
nowepoch = time.Until(epoch)
829829
if nowepoch+time.Microsecond < minNext {
830830
t.Reset(minNext - nowepoch)
831831
} else {

connection_pool/example_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
type Tuple struct {
1212
// Instruct msgpack to pack this struct as array, so no custom packer
1313
// is needed.
14-
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
14+
_msgpack struct{} `msgpack:",asArray"`
1515
Key string
1616
Value string
1717
}
@@ -53,7 +53,7 @@ func ExampleConnectionPool_Select() {
5353
return
5454
}
5555
// Insert a new tuple {"key2", "value2"}.
56-
_, err = conn.Insert(spaceName, &Tuple{Key: "key2", Value: "value2"})
56+
_, err = conn.Insert(spaceName, &Tuple{Key: "key2", Value: "value2"})
5757
if err != nil {
5858
fmt.Printf("Failed to insert: %s", err.Error())
5959
return
@@ -114,7 +114,7 @@ func ExampleConnectionPool_SelectTyped() {
114114
return
115115
}
116116
// Insert a new tuple {"key2", "value2"}.
117-
_, err = conn.Insert(spaceName, &Tuple{Key: "key2", Value: "value2"})
117+
_, err = conn.Insert(spaceName, &Tuple{Key: "key2", Value: "value2"})
118118
if err != nil {
119119
fmt.Printf("Failed to insert: %s", err.Error())
120120
return
@@ -176,7 +176,7 @@ func ExampleConnectionPool_SelectAsync() {
176176
return
177177
}
178178
// Insert a new tuple {"key2", "value2"}.
179-
_, err = conn.Insert(spaceName, &Tuple{Key: "key2", Value: "value2"})
179+
_, err = conn.Insert(spaceName, &Tuple{Key: "key2", Value: "value2"})
180180
if err != nil {
181181
fmt.Printf("Failed to insert: %s", err.Error())
182182
return
@@ -196,7 +196,7 @@ func ExampleConnectionPool_SelectAsync() {
196196
spaceName, indexName, 0, 1, tarantool.IterEq,
197197
[]interface{}{"key2"}, connection_pool.RW)
198198
futs[2] = pool.SelectAsync(
199-
spaceName, indexName, 0, 1,tarantool.IterEq,
199+
spaceName, indexName, 0, 1, tarantool.IterEq,
200200
[]interface{}{"key3"}, connection_pool.RW)
201201
var t []Tuple
202202
err = futs[0].GetTyped(&t)

example_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func ExampleConnection_SelectTyped() {
7070
// response is [{{} 1111 hello world}]
7171
}
7272

73+
//nolint
7374
func ExampleConnection_SelectAsync() {
7475
conn := example_connect()
7576
defer conn.Close()

multi/multi_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ var connOptsMulti = OptsMulti{
2727
func TestConnError_IncorrectParams(t *testing.T) {
2828
multiConn, err := Connect([]string{}, tarantool.Opts{})
2929
if err == nil {
30-
t.Errorf("err is nil with incorrect params")
30+
t.Fatalf("err is nil with incorrect params")
3131
}
3232
if multiConn != nil {
33-
t.Errorf("conn is not nill with incorrect params")
33+
t.Fatalf("conn is not nill with incorrect params")
3434
}
3535
if err.Error() != "addrs should not be empty" {
3636
t.Errorf("incorrect error: %s", err.Error())
3737
}
3838

3939
multiConn, err = ConnectWithOpts([]string{server1}, tarantool.Opts{}, OptsMulti{})
4040
if err == nil {
41-
t.Errorf("err is nil with incorrect params")
41+
t.Fatal("err is nil with incorrect params")
4242
}
4343
if multiConn != nil {
44-
t.Errorf("conn is not nill with incorrect params")
44+
t.Fatal("conn is not nill with incorrect params")
4545
}
4646
if err.Error() != "wrong check timeout, must be greater than 0" {
4747
t.Errorf("incorrect error: %s", err.Error())

queue/example_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/tarantool/go-tarantool/queue"
1717
)
1818

19+
//nolint
1920
// Example demonstrates an operations like Put and Take with queue.
2021
func Example_simpleQueue() {
2122
cfg := queue.Cfg{

queue/queue.go

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ type queueData struct {
339339
result interface{}
340340
}
341341

342+
//nolint
342343
func (qd *queueData) DecodeMsgpack(d *msgpack.Decoder) error {
343344
var err error
344345
var l int

queue/queue_test.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ func TestFifoQueue_Release(t *testing.T) {
630630
func TestTtlQueue(t *testing.T) {
631631
conn, err := Connect(server, opts)
632632
if err != nil {
633-
t.Errorf("Failed to connect: %s", err.Error())
633+
t.Fatalf("Failed to connect: %s", err.Error())
634634
return
635635
}
636636
defer conn.Close()
@@ -683,11 +683,11 @@ func TestTtlQueue(t *testing.T) {
683683
func TestTtlQueue_Put(t *testing.T) {
684684
conn, err := Connect(server, opts)
685685
if err != nil {
686-
t.Errorf("Failed to connect: %s", err.Error())
686+
t.Fatalf("Failed to connect: %s", err.Error())
687687
return
688688
}
689689
if conn == nil {
690-
t.Errorf("conn is nil after Connect")
690+
t.Fatalf("conn is nil after Connect")
691691
return
692692
}
693693
defer conn.Close()
@@ -755,11 +755,11 @@ func TestTtlQueue_Put(t *testing.T) {
755755
func TestUtube_Put(t *testing.T) {
756756
conn, err := Connect(server, opts)
757757
if err != nil {
758-
t.Errorf("Failed to connect: %s", err.Error())
758+
t.Fatalf("Failed to connect: %s", err.Error())
759759
return
760760
}
761761
if conn == nil {
762-
t.Errorf("conn is nil after Connect")
762+
t.Fatalf("conn is nil after Connect")
763763
return
764764
}
765765
defer conn.Close()
@@ -794,16 +794,22 @@ func TestUtube_Put(t *testing.T) {
794794
t.Fatalf("Failed put task to queue: %s", err.Error())
795795
}
796796

797+
errChan := make(chan struct{})
797798
go func() {
798799
t1, err := q.TakeTimeout(2 * time.Second)
799800
if err != nil {
800-
t.Fatalf("Failed to take task from utube: %s", err.Error())
801+
t.Errorf("Failed to take task from utube: %s", err.Error())
802+
errChan <- struct{}{}
803+
return
801804
}
802805

803806
time.Sleep(2 * time.Second)
804807
if err := t1.Ack(); err != nil {
805-
t.Fatalf("Failed to ack task: %s", err.Error())
808+
t.Errorf("Failed to ack task: %s", err.Error())
809+
errChan <- struct{}{}
810+
return
806811
}
812+
close(errChan)
807813
}()
808814

809815
time.Sleep(100 * time.Millisecond)
@@ -817,6 +823,9 @@ func TestUtube_Put(t *testing.T) {
817823
t.Fatalf("Failed to ack task: %s", err.Error())
818824
}
819825
end := time.Now()
826+
if _, ok := <-errChan; ok {
827+
t.Fatalf("One of tasks failed")
828+
}
820829
if math.Abs(float64(end.Sub(start)-2*time.Second)) > float64(200*time.Millisecond) {
821830
t.Fatalf("Blocking time is less than expected: actual = %.2fs, expected = 1s", end.Sub(start).Seconds())
822831
}

response.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Response struct {
1515
buf smallBuf
1616
}
1717

18+
//nolint: unused
1819
func (resp *Response) fill(b []byte) {
1920
resp.buf.b = b
2021
}

schema.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type IndexField struct {
5050
Type string
5151
}
5252

53+
//nolint: varcheck,deadcode
5354
const (
5455
maxSchemas = 10000
5556
spaceSpId = 280

0 commit comments

Comments
 (0)