github.com/drone/runner-go@v1.12.0/client/single_test.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package client
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"testing"
    11  
    12  	"github.com/drone/drone-go/drone"
    13  )
    14  
    15  var noContext = context.Background()
    16  
    17  func TestSingleFlight(t *testing.T) {
    18  	mock := &mockRequestClient{
    19  		out: &drone.Stage{},
    20  		err: errors.New("some random error"),
    21  	}
    22  	client := NewSingleFlight("", "", false)
    23  	client.Client = mock
    24  	out, err := client.Request(noContext, nil)
    25  	if got, want := out, mock.out; got != want {
    26  		t.Errorf("Expect stage returned from request")
    27  	}
    28  	if got, want := err, mock.err; got != want {
    29  		t.Errorf("Expect error returned from request")
    30  	}
    31  }
    32  
    33  func TestSingleFlightPanic(t *testing.T) {
    34  	mock := &mockRequestClientPanic{}
    35  	client := NewSingleFlight("", "", false)
    36  	client.Client = mock
    37  
    38  	defer func() {
    39  		if recover() != nil {
    40  			t.Errorf("Expect Request to recover from panic")
    41  		}
    42  		client.mu.Lock()
    43  		client.mu.Unlock()
    44  	}()
    45  
    46  	client.Request(noContext, nil)
    47  }
    48  
    49  func TestSingleFlightCancel(t *testing.T) {
    50  	ctx, cancel := context.WithCancel(noContext)
    51  	cancel()
    52  	client := NewSingleFlight("", "", false)
    53  	client.Request(ctx, nil)
    54  }
    55  
    56  // mock client that returns a static stage and error
    57  // from the request method.
    58  type mockRequestClient struct {
    59  	Client
    60  
    61  	out *drone.Stage
    62  	err error
    63  }
    64  
    65  func (m *mockRequestClient) Request(ctx context.Context, args *Filter) (*drone.Stage, error) {
    66  	return m.out, m.err
    67  }
    68  
    69  // mock client that returns panics when the request
    70  // method is invoked.
    71  type mockRequestClientPanic struct {
    72  	Client
    73  }
    74  
    75  func (m *mockRequestClientPanic) Request(ctx context.Context, args *Filter) (*drone.Stage, error) {
    76  	panic("method not implemented")
    77  }