github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/track/track_test.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package track
    19  
    20  import (
    21  	"net/http"
    22  	"testing"
    23  
    24  	"fmt"
    25  	"net/http/httptest"
    26  	"sync"
    27  	"time"
    28  )
    29  
    30  type mockTimeoutRoundTripper struct {
    31  }
    32  
    33  func (r mockTimeoutRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    34  	for {
    35  		<-time.After(timeout + 1*time.Minute)
    36  		fmt.Print("after timeout")
    37  		return nil, nil
    38  	}
    39  }
    40  
    41  type mockRoundTripper struct {
    42  }
    43  
    44  func (r mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    45  	return httptest.NewRecorder().Result(), nil
    46  }
    47  
    48  func TestTelemetryDisabled(t *testing.T) {
    49  	gaHTTPTransport = mockRoundTripper{}
    50  	telemetryEnabled = false
    51  	telemetryLogEnabled = false
    52  	wg := &sync.WaitGroup{}
    53  	wg.Add(1)
    54  	expected := send("foo", "bar", "baz", "test", wg)
    55  	wg.Wait()
    56  
    57  	if expected {
    58  		t.Error("Expected to not send request")
    59  	}
    60  }
    61  
    62  func TestTimeout(t *testing.T) {
    63  	telemetryEnabled = true
    64  	telemetryLogEnabled = false
    65  	gaHTTPTransport = mockTimeoutRoundTripper{}
    66  	wg := &sync.WaitGroup{}
    67  	wg.Add(1)
    68  	expected := send("foo", "bar", "baz", "test", wg)
    69  	wg.Wait()
    70  
    71  	if expected {
    72  		t.Error("Expected request to timeout")
    73  	}
    74  }
    75  
    76  func TestSend(t *testing.T) {
    77  	gaHTTPTransport = mockRoundTripper{}
    78  	telemetryEnabled = true
    79  	telemetryLogEnabled = false
    80  	wg := &sync.WaitGroup{}
    81  	wg.Add(1)
    82  	expected := send("foo", "bar", "baz", "test", wg)
    83  	wg.Wait()
    84  
    85  	if !expected {
    86  		t.Error("Expected to send request")
    87  	}
    88  }