Skip to content

Commit

Permalink
Enforce inlining functions with a single error return in `require.NoE…
Browse files Browse the repository at this point in the history
…rror` (ava-labs#1500)
  • Loading branch information
dhrubabasu authored May 30, 2023
1 parent 7403188 commit b66e25e
Show file tree
Hide file tree
Showing 100 changed files with 1,230 additions and 2,329 deletions.
4 changes: 1 addition & 3 deletions api/admin/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ func TestLoadVMsSuccess(t *testing.T) {

// execute test
reply := LoadVMsReply{}
err := resources.admin.LoadVMs(&http.Request{}, nil, &reply)

require.NoError(resources.admin.LoadVMs(&http.Request{}, nil, &reply))
require.Equal(expectedVMRegistry, reply.NewVMs)
require.NoError(err)
}

// Tests behavior for LoadVMs if we fail to reload vms.
Expand Down
3 changes: 1 addition & 2 deletions api/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ func TestRevokeToken(t *testing.T) {
tokenStr, err := auth.NewToken(testPassword, defaultTokenLifespan, endpoints)
require.NoError(err)

err = auth.RevokeToken(tokenStr, testPassword)
require.NoError(err)
require.NoError(auth.RevokeToken(tokenStr, testPassword))
require.Len(auth.revoked, 1)
}

Expand Down
3 changes: 1 addition & 2 deletions api/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,8 @@ func TestDeadlockRegression(t *testing.T) {

for i := 0; i < 1000; i++ {
lock.Lock()
err = h.RegisterHealthCheck(fmt.Sprintf("check-%d", i), check)
require.NoError(h.RegisterHealthCheck(fmt.Sprintf("check-%d", i), check))
lock.Unlock()
require.NoError(err)
}

awaitHealthy(t, h, true)
Expand Down
25 changes: 10 additions & 15 deletions api/keystore/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ func TestServiceCreateUser(t *testing.T) {
s := service{ks: ks.(*keystore)}

{
err := s.CreateUser(nil, &api.UserPass{
require.NoError(s.CreateUser(nil, &api.UserPass{
Username: "bob",
Password: strongPassword,
}, &api.EmptyReply{})
require.NoError(err)
}, &api.EmptyReply{}))
}

{
Expand Down Expand Up @@ -123,11 +122,10 @@ func TestServiceCreateDuplicate(t *testing.T) {
s := service{ks: ks.(*keystore)}

{
err := s.CreateUser(nil, &api.UserPass{
require.NoError(s.CreateUser(nil, &api.UserPass{
Username: "bob",
Password: strongPassword,
}, &api.EmptyReply{})
require.NoError(err)
}, &api.EmptyReply{}))
}

{
Expand Down Expand Up @@ -161,11 +159,10 @@ func TestServiceUseBlockchainDB(t *testing.T) {
s := service{ks: ks.(*keystore)}

{
err := s.CreateUser(nil, &api.UserPass{
require.NoError(s.CreateUser(nil, &api.UserPass{
Username: "bob",
Password: strongPassword,
}, &api.EmptyReply{})
require.NoError(err)
}, &api.EmptyReply{}))
}

{
Expand Down Expand Up @@ -193,11 +190,10 @@ func TestServiceExportImport(t *testing.T) {
s := service{ks: ks.(*keystore)}

{
err := s.CreateUser(nil, &api.UserPass{
require.NoError(s.CreateUser(nil, &api.UserPass{
Username: "bob",
Password: strongPassword,
}, &api.EmptyReply{})
require.NoError(err)
}, &api.EmptyReply{}))
}

{
Expand Down Expand Up @@ -243,15 +239,14 @@ func TestServiceExportImport(t *testing.T) {
}

{
err := newS.ImportUser(nil, &ImportUserArgs{
require.NoError(newS.ImportUser(nil, &ImportUserArgs{
UserPass: api.UserPass{
Username: "bob",
Password: strongPassword,
},
User: exportReply.User,
Encoding: encoding,
}, &api.EmptyReply{})
require.NoError(err)
}, &api.EmptyReply{}))
}

{
Expand Down
7 changes: 2 additions & 5 deletions chains/atomic/gsharedmemory/shared_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ func TestInterface(t *testing.T) {

test(t, chainID0, chainID1, sm0, sm1, testDB)

err := conn0.Close()
require.NoError(err)

err = conn1.Close()
require.NoError(err)
require.NoError(conn0.Close())
require.NoError(conn1.Close())
}
}

Expand Down
109 changes: 39 additions & 70 deletions chains/atomic/test_shared_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ var SharedMemoryTests = []func(t *testing.T, chainID0, chainID1 ids.ID, sm0, sm1
func TestSharedMemoryPutAndGet(t *testing.T, chainID0, chainID1 ids.ID, sm0, sm1 SharedMemory, _ database.Database) {
require := require.New(t)

err := sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
require.NoError(sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
Key: []byte{0},
Value: []byte{1},
}}}})

require.NoError(err)
}}}}))

values, err := sm1.Get(chainID0, [][]byte{{0}})
require.NoError(err)
Expand Down Expand Up @@ -73,12 +71,11 @@ func TestSharedMemoryLargePutGetAndRemove(t *testing.T, chainID0, chainID1 ids.I
keys = append(keys, key)
}

err = sm0.Apply(map[ids.ID]*Requests{
require.NoError(sm0.Apply(map[ids.ID]*Requests{
chainID1: {
PutRequests: elems,
},
})
require.NoError(err)
}))

values, err := sm1.Get(
chainID0,
Expand All @@ -89,37 +86,33 @@ func TestSharedMemoryLargePutGetAndRemove(t *testing.T, chainID0, chainID1 ids.I
require.Equal(elems[i].Value, value)
}

err = sm1.Apply(map[ids.ID]*Requests{
require.NoError(sm1.Apply(map[ids.ID]*Requests{
chainID0: {
RemoveRequests: keys,
},
})

require.NoError(err)
}))
}

func TestSharedMemoryIndexed(t *testing.T, chainID0, chainID1 ids.ID, sm0, sm1 SharedMemory, _ database.Database) {
require := require.New(t)

err := sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
require.NoError(sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
Key: []byte{0},
Value: []byte{1},
Traits: [][]byte{
{2},
{3},
},
}}}})
require.NoError(err)
}}}}))

err = sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
require.NoError(sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
Key: []byte{4},
Value: []byte{5},
Traits: [][]byte{
{2},
{3},
},
}}}})
require.NoError(err)
}}}}))

values, _, _, err := sm0.Indexed(chainID1, [][]byte{{2}}, nil, nil, 1)
require.NoError(err)
Expand Down Expand Up @@ -183,8 +176,7 @@ func TestSharedMemoryLargeIndexed(t *testing.T, chainID0, chainID1 ids.ID, sm0,
})
}

err = sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: elems}})
require.NoError(err)
require.NoError(sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: elems}}))

values, _, _, err := sm1.Indexed(chainID0, allTraits, nil, nil, len(elems)+1)
require.NoError(err)
Expand All @@ -207,11 +199,10 @@ func TestSharedMemoryCantDuplicatePut(t *testing.T, _, chainID1 ids.ID, sm0, _ S
// TODO: require error to be errDuplicatedOperation
require.Error(err) //nolint:forbidigo // currently returns grpc errors too

err = sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
require.NoError(sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
Key: []byte{0},
Value: []byte{1},
}}}})
require.NoError(err)
}}}}))

err = sm0.Apply(map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
Key: []byte{0},
Expand All @@ -224,36 +215,31 @@ func TestSharedMemoryCantDuplicatePut(t *testing.T, _, chainID1 ids.ID, sm0, _ S
func TestSharedMemoryCantDuplicateRemove(t *testing.T, _, chainID1 ids.ID, sm0, _ SharedMemory, _ database.Database) {
require := require.New(t)

err := sm0.Apply(map[ids.ID]*Requests{chainID1: {RemoveRequests: [][]byte{{0}}}})
require.NoError(err)
require.NoError(sm0.Apply(map[ids.ID]*Requests{chainID1: {RemoveRequests: [][]byte{{0}}}}))

err = sm0.Apply(map[ids.ID]*Requests{chainID1: {RemoveRequests: [][]byte{{0}}}})
err := sm0.Apply(map[ids.ID]*Requests{chainID1: {RemoveRequests: [][]byte{{0}}}})
// TODO: require error to be errDuplicatedOperation
require.Error(err) //nolint:forbidigo // currently returns grpc errors too
}

func TestSharedMemoryCommitOnPut(t *testing.T, _, chainID1 ids.ID, sm0, _ SharedMemory, db database.Database) {
require := require.New(t)

err := db.Put([]byte{1}, []byte{2})
require.NoError(err)
require.NoError(db.Put([]byte{1}, []byte{2}))

batch := db.NewBatch()

err = batch.Put([]byte{0}, []byte{1})
require.NoError(err)
require.NoError(batch.Put([]byte{0}, []byte{1}))

err = batch.Delete([]byte{1})
require.NoError(err)
require.NoError(batch.Delete([]byte{1}))

err = sm0.Apply(
require.NoError(sm0.Apply(
map[ids.ID]*Requests{chainID1: {PutRequests: []*Element{{
Key: []byte{0},
Value: []byte{1},
}}}},
batch,
)
require.NoError(err)
))

val, err := db.Get([]byte{0})
require.NoError(err)
Expand All @@ -267,22 +253,18 @@ func TestSharedMemoryCommitOnPut(t *testing.T, _, chainID1 ids.ID, sm0, _ Shared
func TestSharedMemoryCommitOnRemove(t *testing.T, _, chainID1 ids.ID, sm0, _ SharedMemory, db database.Database) {
require := require.New(t)

err := db.Put([]byte{1}, []byte{2})
require.NoError(err)
require.NoError(db.Put([]byte{1}, []byte{2}))

batch := db.NewBatch()

err = batch.Put([]byte{0}, []byte{1})
require.NoError(err)
require.NoError(batch.Put([]byte{0}, []byte{1}))

err = batch.Delete([]byte{1})
require.NoError(err)
require.NoError(batch.Delete([]byte{1}))

err = sm0.Apply(
require.NoError(sm0.Apply(
map[ids.ID]*Requests{chainID1: {RemoveRequests: [][]byte{{0}}}},
batch,
)
require.NoError(err)
))

val, err := db.Get([]byte{0})
require.NoError(err)
Expand All @@ -299,8 +281,7 @@ func TestPutAndRemoveBatch(t *testing.T, chainID0, _ ids.ID, _, sm1 SharedMemory

batch := db.NewBatch()

err := batch.Put([]byte{0}, []byte{1})
require.NoError(err)
require.NoError(batch.Put([]byte{0}, []byte{1}))

batchChainsAndInputs := make(map[ids.ID]*Requests)

Expand All @@ -314,9 +295,7 @@ func TestPutAndRemoveBatch(t *testing.T, chainID0, _ ids.ID, _, sm1 SharedMemory
RemoveRequests: byteArr,
}

err = sm1.Apply(batchChainsAndInputs, batch)

require.NoError(err)
require.NoError(sm1.Apply(batchChainsAndInputs, batch))

val, err := db.Get([]byte{0})
require.NoError(err)
Expand Down Expand Up @@ -348,24 +327,19 @@ func TestSharedMemoryLargeBatchSize(t *testing.T, _, chainID1 ids.ID, sm0, _ Sha
value := bytes[:elementSize]
bytes = bytes[elementSize:]

err := batch.Put(key, value)
require.NoError(err)
require.NoError(batch.Put(key, value))
}

err = db.Put([]byte{1}, []byte{2})
require.NoError(err)
require.NoError(db.Put([]byte{1}, []byte{2}))

err = batch.Put([]byte{0}, []byte{1})
require.NoError(err)
require.NoError(batch.Put([]byte{0}, []byte{1}))

err = batch.Delete([]byte{1})
require.NoError(err)
require.NoError(batch.Delete([]byte{1}))

err = sm0.Apply(
require.NoError(sm0.Apply(
map[ids.ID]*Requests{chainID1: {RemoveRequests: [][]byte{{0}}}},
batch,
)
require.NoError(err)
))

val, err := db.Get([]byte{0})
require.NoError(err)
Expand All @@ -382,16 +356,13 @@ func TestSharedMemoryLargeBatchSize(t *testing.T, _, chainID1 ids.ID, sm0, _ Sha
key := bytes[:elementSize]
bytes = bytes[pairSize:]

err := batch.Delete(key)
require.NoError(err)
require.NoError(batch.Delete(key))
}

err = sm0.Apply(
require.NoError(sm0.Apply(
map[ids.ID]*Requests{chainID1: {RemoveRequests: [][]byte{{1}}}},
batch,
)

require.NoError(err)
))

batch.Reset()

Expand All @@ -400,8 +371,7 @@ func TestSharedMemoryLargeBatchSize(t *testing.T, _, chainID1 ids.ID, sm0, _ Sha
key := bytes[:elementSize]
bytes = bytes[pairSize:]

err := batch.Delete(key)
require.NoError(err)
require.NoError(batch.Delete(key))
}

batchChainsAndInputs := make(map[ids.ID]*Requests)
Expand All @@ -416,9 +386,8 @@ func TestSharedMemoryLargeBatchSize(t *testing.T, _, chainID1 ids.ID, sm0, _ Sha
RemoveRequests: byteArr,
}

err = sm0.Apply(
require.NoError(sm0.Apply(
batchChainsAndInputs,
batch,
)
require.NoError(err)
))
}
Loading

0 comments on commit b66e25e

Please sign in to comment.