Skip to content

Commit

Permalink
more complex lint fixes
Browse files Browse the repository at this point in the history
runHandlers need a proper testConnection for progress tests
  • Loading branch information
tphoney committed Aug 22, 2024
1 parent 2203892 commit 7d0dfd8
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 63 deletions.
13 changes: 6 additions & 7 deletions auth/nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -217,10 +218,8 @@ func TestNATSConnect(t *testing.T) {
t.Errorf("Reconnecting took too long, expected <3s got: %v", time.Since(start).String())
}

switch err.(type) {
case MaxRetriesError:
// This is good
default:
var maxRetriesError MaxRetriesError
if !errors.As(err, &maxRetriesError) {
t.Errorf("Unknown error type %T: %v", err, err)
}
})
Expand All @@ -243,8 +242,8 @@ func TestNATSConnect(t *testing.T) {

_, err = o.Connect()

switch err.(type) {
case MaxRetriesError:
var maxRetriesError MaxRetriesError
if errors.As(err, &maxRetriesError) {
// Make sure we have only got one token, not three
currentToken, err := o.TokenClient.GetJWT()

Expand All @@ -255,7 +254,7 @@ func TestNATSConnect(t *testing.T) {
if currentToken != startToken {
t.Error("Tokens have changed")
}
default:
} else {
t.Errorf("Unknown error type %T", err)
}
})
Expand Down
4 changes: 3 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sdp

import (
"errors"
"fmt"

"github.com/google/uuid"
Expand Down Expand Up @@ -41,7 +42,8 @@ func (e *QueryError) Error() string {
// NewQueryError converts a regular error to a QueryError of type
// OTHER. If the input error is already a QueryError then it is preserved
func NewQueryError(err error) *QueryError {
if sdpErr, ok := err.(*QueryError); ok {
var sdpErr *QueryError
if errors.As(err, &sdpErr) {
return sdpErr
}

Expand Down
7 changes: 3 additions & 4 deletions items.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sdp

import (
"context"
"crypto/sha1"
"crypto/sha256"
"encoding/base32"
"encoding/json"
"errors"
Expand Down Expand Up @@ -525,7 +525,7 @@ func sanitizeInterface(i interface{}, sortArrays bool, customTransforms Transfor
t = v.Type()
}

switch v.Kind() {
switch v.Kind() { // nolint:exhaustive // we fall through to the default case
case reflect.Bool:
return v.Bool()
case reflect.Int:
Expand Down Expand Up @@ -643,11 +643,10 @@ func sortInterfaceArray(input []interface{}) {
}

func hashSum(b []byte) string {
var shaSum [20]byte
var paddedEncoding *base32.Encoding
var unpaddedEncoding *base32.Encoding

shaSum = sha1.Sum(b)
shaSum := sha256.Sum256(b)

// We need to specify a custom encoding here since dGraph has fairly strict
// requirements about what name a variable can have
Expand Down
6 changes: 3 additions & 3 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func TestNewAuthMiddleware(t *testing.T) {
ExpectedCode: http.StatusOK,
},
{
Name: "with no token on a non-bypasssed path",
Name: "with no token on a non-bypassed path",
Path: "/",
AuthConfig: bypassHealthConfig,
ExpectedCode: http.StatusUnauthorized,
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestNewAuthMiddleware(t *testing.T) {
}))

rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", test.Path, nil)
req, err := http.NewRequestWithContext(context.Background(), "GET", test.Path, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -458,7 +458,7 @@ func BenchmarkAuthMiddleware(b *testing.B) {
for i := 0; i < b.N; i++ {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest("GET", "/", nil)
req, err := http.NewRequestWithContext(context.Background(), "GET", "/", nil)

if err != nil {
b.Fatal(err)
Expand Down
26 changes: 19 additions & 7 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ func (rs *ResponseSender) Start(ctx context.Context, ec EncodedConnection, respo

if rs.connection != nil {
// Send the initial response
rs.connection.Publish(
err := rs.connection.Publish(
ctx,
rs.ResponseSubject,
&QueryResponse{ResponseType: &QueryResponse_Response{Response: &resp}},
)
if err != nil {
log.WithContext(ctx).WithError(err).Error("Error publishing initial response")
}
}

rs.monitorRunning.Add(1)
Expand Down Expand Up @@ -148,11 +151,14 @@ func (rs *ResponseSender) killWithResponse(ctx context.Context, r *Response) {
if rs.connection != nil {
if r != nil {
// Send the final response
rs.connection.Publish(ctx, rs.ResponseSubject, &QueryResponse{
err := rs.connection.Publish(ctx, rs.ResponseSubject, &QueryResponse{
ResponseType: &QueryResponse_Response{
Response: r,
},
})
if err != nil {
log.WithContext(ctx).WithError(err).Error("Error publishing final response")
}
}
}
}
Expand Down Expand Up @@ -460,7 +466,7 @@ func (qp *QueryProgress) Start(ctx context.Context, ec EncodedConnection, itemCh
}
}

qp.querySub, err = ec.Subscribe(qp.Query.Subject(), NewQueryResponseHandler("", func(ctx context.Context, qr *QueryResponse) {
qp.querySub, err = ec.Subscribe(qp.Query.Subject(), NewQueryResponseHandler("", func(ctx context.Context, qr *QueryResponse) { //nolint:contextcheck // we pass the context in the func
log.WithContext(ctx).WithFields(log.Fields{
"response": qr,
}).Trace("Received response")
Expand Down Expand Up @@ -545,7 +551,10 @@ func (qp *QueryProgress) Drain() {
}

// Close the item and error subscriptions
unsubscribeGracefully(qp.querySub)
err := unsubscribeGracefully(qp.querySub)
if err != nil {
log.WithContext(qp.requestCtx).WithError(err).Error("Error unsubscribing from query subject")
}

qp.chanMutex.Lock()
defer qp.chanMutex.Unlock()
Expand Down Expand Up @@ -579,7 +588,10 @@ func (qp *QueryProgress) Done() <-chan struct{} {
//
// Returns a boolean indicating whether the cancellation needed to be forced
func (qp *QueryProgress) Cancel(ctx context.Context, ec EncodedConnection) bool {
qp.AsyncCancel(ec)
err := qp.AsyncCancel(ec)
if err != nil {
log.WithContext(ctx).WithError(err).Error("Error cancelling request")
}

select {
case <-qp.Done():
Expand Down Expand Up @@ -724,10 +736,10 @@ func (qp *QueryProgress) ProcessResponse(ctx context.Context, response *Response

monitorContext, monitorCancel := context.WithCancel(context.Background())

responder.SetMonitorContext(monitorContext, monitorCancel)
responder.SetMonitorContext(monitorContext, monitorCancel) // nolint: contextcheck // we expect a new response

// Create a goroutine to watch for a stalled connection
go stallMonitor(monitorContext, timeout, responder, qp)
go stallMonitor(monitorContext, timeout, responder, qp) // nolint: contextcheck // we expect a new response
}

// Finally check to see if this was the final request and if so drain
Expand Down
Loading

0 comments on commit 7d0dfd8

Please sign in to comment.