Skip to content

Commit

Permalink
Simplify error handling in goroutines.
Browse files Browse the repository at this point in the history
Just use t.Error()
  • Loading branch information
robertodauria committed May 17, 2022
1 parent adbb33b commit 7aa2906
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 84 deletions.
71 changes: 21 additions & 50 deletions internal/download/download_test.go
Expand Up @@ -92,10 +92,12 @@ func TestReadBinary(t *testing.T) {
NextReaderMessageType: websocket.BinaryMessage,
MessageByteArray: []byte("12345678"),
}
errch := make(chan error, 1)
go func(errch chan<- error) {
errch <- Run(ctx, &conn, outch)
}(errch)
go func() {
err := Run(ctx, &conn, outch)
if err != nil {
t.Errorf("error: %v", err)
}
}()
prev := spec.Measurement{
AppInfo: &spec.AppInfo{},
}
Expand All @@ -117,9 +119,6 @@ func TestReadBinary(t *testing.T) {
}
prev = m
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}

func TestSetReadDeadlineError(t *testing.T) {
Expand All @@ -134,21 +133,15 @@ func TestSetReadDeadlineError(t *testing.T) {
MessageByteArray: []byte("{}"),
SetReadDeadlineResult: mockedErr,
}
errch := make(chan error, 1)
go func(errch chan<- error) {
go func() {
for range outch {
errch <- errors.New("We didn't expect measurements here")
return
t.Error("We didn't expect measurements here")
}
errch <- nil
}(errch)
}()
err := Run(ctx, &conn, outch)
if err != mockedErr {
t.Fatal("Not the error that we were expecting")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}

func TestReadMessageError(t *testing.T) {
Expand All @@ -163,21 +156,15 @@ func TestReadMessageError(t *testing.T) {
MessageByteArray: []byte("{}"),
NextReaderResult: mockedErr,
}
errch := make(chan error, 1)
go func(errch chan<- error) {
go func() {
for range outch {
errch <- errors.New("We didn't expect measurements here")
return
t.Error("We didn't expect measurements here")
}
errch <- nil
}(errch)
}()
err := Run(ctx, &conn, outch)
if err != mockedErr {
t.Fatal("Not the error that we were expecting")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}

func TestReaderError(t *testing.T) {
Expand All @@ -191,21 +178,16 @@ func TestReaderError(t *testing.T) {
NextReaderMessageType: websocket.TextMessage,
NextReaderMustFail: true,
}
errch := make(chan error, 1)
go func(errch chan<- error) {
go func() {
for range outch {
errch <- errors.New("We didn't expect measurements here")
t.Error("We didn't expect measurements here")
return
}
errch <- nil
}(errch)
}()
err := Run(ctx, &conn, outch)
if err != mocks.ErrReadFailed {
t.Fatal("Not the error that we were expecting")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
// Test when type is websocket.BinaryMessage
outch = make(chan spec.Measurement)
ctx, cancel = context.WithTimeout(
Expand All @@ -216,21 +198,15 @@ func TestReaderError(t *testing.T) {
NextReaderMessageType: websocket.BinaryMessage,
NextReaderMustFail: true,
}
errch = make(chan error, 1)
go func(errch chan<- error) {
go func() {
for range outch {
errch <- errors.New("We didn't expect measurements here")
return
t.Error("We didn't expect measurements here")
}
errch <- nil
}(errch)
}()
err = Run(ctx, &conn, outch)
if err != mocks.ErrReadFailed {
t.Fatal("Not the error that we were expecting")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}

func TestReadInvalidJSON(t *testing.T) {
Expand All @@ -243,19 +219,14 @@ func TestReadInvalidJSON(t *testing.T) {
NextReaderMessageType: websocket.TextMessage,
MessageByteArray: []byte("{"),
}
errch := make(chan error, 1)
go func(errch chan<- error) {
go func() {
for range outch {
errch <- errors.New("We didn't expect measurements here")
t.Error("We didn't expect measurements here")
return
}
errch <- nil
}(errch)
}()
err := Run(ctx, &conn, outch)
if err == nil {
t.Fatal("We expected to have an error here")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}
49 changes: 15 additions & 34 deletions internal/upload/upload_test.go
Expand Up @@ -22,10 +22,12 @@ func TestNormal(t *testing.T) {
MessageByteArray: []byte("{}"),
ReadMessageType: websocket.TextMessage,
}
errch := make(chan error)
go func(errch chan<- error) {
errch <- Run(ctx, &conn, outch)
}(errch)
go func() {
err := Run(ctx, &conn, outch)
if err != nil {
t.Errorf("error: %v", err)
}
}()
tot := 0
// Drain the channel and count the number of Measurements read.
for range outch {
Expand All @@ -34,9 +36,6 @@ func TestNormal(t *testing.T) {
if tot <= 0 {
t.Fatal("Expected at least one message")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}

func TestSetReadDeadlineError(t *testing.T) {
Expand Down Expand Up @@ -130,22 +129,16 @@ func TestMakePreparedMessageError(t *testing.T) {
return nil, mockedErr
}
conn := mocks.Conn{}
errch := make(chan error)
go func(errch chan<- error) {
go func() {
for range outch {
errch <- errors.New("Did not expect messages here")
return
t.Error("Did not expect messages here")
}
errch <- nil
}(errch)
}()
err := upload(ctx, &conn, outch)
makePreparedMessage = savedFunc
if err != mockedErr {
t.Fatal("Not the error we expected")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}

func TestSetWriteDeadlineError(t *testing.T) {
Expand All @@ -158,21 +151,15 @@ func TestSetWriteDeadlineError(t *testing.T) {
conn := mocks.Conn{
SetWriteDeadlineResult: mockedErr,
}
errch := make(chan error, 1)
go func(errch chan<- error) {
go func() {
for range outch {
errch <- errors.New("Did not expect messages here")
return
t.Error("Did not expect messages here")
}
errch <- nil
}(errch)
}()
err := upload(ctx, &conn, outch)
if err != mockedErr {
t.Fatal("Not the error we expected")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}

func TestWritePreparedMessageError(t *testing.T) {
Expand All @@ -185,19 +172,13 @@ func TestWritePreparedMessageError(t *testing.T) {
conn := mocks.Conn{
WritePreparedMessageResult: mockedErr,
}
errch := make(chan error, 1)
go func(errch chan<- error) {
go func() {
for range outch {
errch <- errors.New("Did not expect messages here")
return
t.Error("Did not expect messages here")
}
errch <- nil
}(errch)
}()
err := upload(ctx, &conn, outch)
if err != mockedErr {
t.Fatal("Not the error we expected")
}
if err := <-errch; err != nil {
t.Fatal(err)
}
}

0 comments on commit 7aa2906

Please sign in to comment.