Skip to content

Commit

Permalink
gcsworker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Brophy committed Dec 7, 2017
1 parent 559acf9 commit 5ed9c07
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dummyworker/dummyworker.go
Expand Up @@ -32,6 +32,8 @@ type Worker struct {
// Start satisfies the blaster.Starter interface
func (w *Worker) Start(ctx context.Context, raw map[string]interface{}) error {

// notest

var config workerConfig
if err := mapstructure.Decode(raw, &config); err != nil {
return err
Expand All @@ -52,6 +54,8 @@ func (w *Worker) Start(ctx context.Context, raw map[string]interface{}) error {
// Send satisfies the blaster.Worker interface
func (w *Worker) Send(ctx context.Context, raw map[string]interface{}) (map[string]interface{}, error) {

// notest

var payload payloadConfig
if err := mapstructure.Decode(raw, &payload); err != nil {
return map[string]interface{}{"status": "Error decoding payload"}, err
Expand Down
4 changes: 4 additions & 0 deletions gcsworker/gcsworker.go
Expand Up @@ -30,6 +30,7 @@ type Worker struct {

// Start satisfies the blaster.Starter interface
func (w *Worker) Start(ctx context.Context, payload map[string]interface{}) error {
// notest
src, err := google.DefaultTokenSource(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -63,14 +64,17 @@ func (w *Worker) Send(ctx context.Context, raw map[string]interface{}) (map[stri
ue, ok := err.(*url.Error)
switch {
case response != nil:
// notest
status = response.StatusCode
case ok && ue.Err == context.DeadlineExceeded:
status = "Timeout"
case ok && ue.Err == context.Canceled:
status = "Cancelled"
case ok:
// notest
status = ue.Err.Error()
default:
// notest
status = err.Error()
}
return map[string]interface{}{"status": status}, err
Expand Down
184 changes: 184 additions & 0 deletions gcsworker/gcsworker_test.go
@@ -0,0 +1,184 @@
package gcsworker

import (
"context"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
)

func TestSend(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method: %s", r.Method)
}
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
}
w := New()
w.(*Worker).client = http.DefaultClient
response, err := w.Send(context.Background(), payload)
expected := map[string]interface{}{
"status": 200,
}
if err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestPost(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Unexpected method: %s", r.Method)
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if string(b) != "abc" {
t.Errorf("Unexpected body: %s", string(b))
}
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "POST",
"url": ts.URL,
"body": "abc",
}
w := New()
w.(*Worker).client = http.DefaultClient
response, err := w.Send(context.Background(), payload)
expected := map[string]interface{}{
"status": 200,
}
if err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestHeaders(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method: %s", r.Method)
}
if r.Header.Get("a") != "b" || r.Header.Get("c") != "d" {
t.Errorf("Unexpected: a=%s, c=%s", r.Header.Get("a"), r.Header.Get("c"))
}
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
"headers": map[string]string{
"a": "b",
"c": "d",
},
}
w := New()
w.(*Worker).client = http.DefaultClient
response, err := w.Send(context.Background(), payload)
expected := map[string]interface{}{
"status": 200,
}
if err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - error"))
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
}
w := New()
w.(*Worker).client = http.DefaultClient
response, err := w.Send(context.Background(), payload)
expected := map[string]interface{}{
"status": 500,
}
if err == nil || err.Error() != "non 200 status" {
log.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestErrorTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(time.Second)
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
}
ctx, _ := context.WithTimeout(context.Background(), time.Millisecond)
w := New()
w.(*Worker).client = http.DefaultClient
response, err := w.Send(ctx, payload)
expected := map[string]interface{}{
"status": "Timeout",
}
if err == nil || !strings.HasSuffix(err.Error(), "context deadline exceeded") {
log.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

func TestErrorCancel(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(time.Second)
}))
defer ts.Close()

payload := map[string]interface{}{
"method": "GET",
"url": ts.URL,
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
go func() {
cancel()
}()
w := New()
w.(*Worker).client = http.DefaultClient
response, err := w.Send(ctx, payload)
expected := map[string]interface{}{
"status": "Cancelled",
}
if err == nil || !strings.HasSuffix(err.Error(), "context canceled") {
log.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("Unexpected: %#v", response)
}
}

0 comments on commit 5ed9c07

Please sign in to comment.