Skip to content

Commit

Permalink
Use test.v flag instead of TestMode
Browse files Browse the repository at this point in the history
  • Loading branch information
gfr10598 committed Jun 26, 2018
1 parent 325dadd commit f8f686e
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 24 deletions.
2 changes: 0 additions & 2 deletions cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ type Config struct {
// client to be used for cloud API calls. Allows injection of fake for testing.
Client *http.Client
Options []option.ClientOption

TestMode bool
}

// BQConfig provides a generic config suitable for BigQuery clients.
Expand Down
12 changes: 10 additions & 2 deletions cloud/tq/queuehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tq

import (
"errors"
"flag"
"io"
"log"
"math/rand"
Expand All @@ -16,6 +17,13 @@ import (
"google.golang.org/appengine/taskqueue"
)

// testMode is set IFF the test.v flag is defined, as it is in all go testing.T tests.
var testMode bool

func init() {
testMode = flag.Lookup("test.v") != nil
}

// ChannelQueueHandler is an autonomous queue handler running in a go
// routine, fed by a channel.
type ChannelQueueHandler struct {
Expand Down Expand Up @@ -86,7 +94,7 @@ func (qh *ChannelQueueHandler) waitForEmptyQueue() {
stats, err := GetTaskqueueStats(qh.Config, qh.Queue)
if err != nil {
if err == io.EOF {
if !qh.TestMode {
if !testMode {
log.Println(err, "GetTaskqueueStats returned EOF - test client?")
}
return
Expand Down Expand Up @@ -171,7 +179,7 @@ func (qh *ChannelQueueHandler) handleLoop(next api.TaskPipe, bucketOpts ...optio
task.SetError(err, "ProcessOneRequest")
continue
}
if qh.TestMode {
if testMode {
log.Println("test mode")
n = 1
}
Expand Down
6 changes: 2 additions & 4 deletions cloud/tq/queuehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ func init() {
func TestChannelQueueHandler(t *testing.T) {
client, counter := cloud.DryRunClient()
config := cloud.Config{
Project: "mlab-testing",
Client: client,
Options: nil,
TestMode: true}
Project: "mlab-testing",
Client: client}
cqh, err := tq.NewChannelQueueHandler(config, "test-queue", nil)
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions cloud/tq/tq.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// *******************************************************************
// Queuer handles queueing of reprocessing requests
// QueueHandler handles queueing of reprocessing requests
// *******************************************************************

// Errors associated with Queuing
Expand Down Expand Up @@ -132,7 +132,7 @@ func (qh QueueHandler) PostOneTask(bucket, fn string) error {
// if there are recoverable errors.
func (qh *QueueHandler) postWithRetry(bucket, filepath string) error {
backoff := 5 * time.Second
if qh.TestMode {
if testMode {
backoff = 100 * time.Millisecond
}
var err error
Expand Down
6 changes: 2 additions & 4 deletions cmd/gardener/gardener.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,8 @@ func dispatcherFromEnv(client *http.Client) (*dispatch.Dispatcher, error) {
}

config := cloud.Config{
Project: env.Project,
Client: client,
Options: nil,
TestMode: false}
Project: env.Project,
Client: client}
return dispatch.NewDispatcher(config, env.QueueBase, env.NumQueues, env.StartDate, ds)
}

Expand Down
13 changes: 11 additions & 2 deletions dispatch/deduphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dispatch
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
Expand All @@ -18,6 +19,14 @@ import (
"github.com/m-lab/go/bqext"
)

// testMode is set IFF the test.v flag is defined, as it is in all go testing.T tests.
// Use with caution!
var testMode bool

func init() {
testMode = flag.Lookup("test.v") != nil
}

// Dedup related errors.
var (
ErrTableNotFound = errors.New("Table not found")
Expand All @@ -41,7 +50,7 @@ func (dh *DedupHandler) Response() <-chan error {
}

// WaitForStableTable loops checking until table exists and has no streaming buffer.
func WaitForStableTable(tt *bigquery.Table, testMode bool) error {
func WaitForStableTable(tt *bigquery.Table) error {
errorTimeout := 2 * time.Minute
if testMode {
errorTimeout = 100 * time.Millisecond
Expand Down Expand Up @@ -114,7 +123,7 @@ func (dh *DedupHandler) waitAndDedup(ds *bqext.Dataset, task state.Task) error {
// First wait for the source table's streaming buffer to be integrated.
// This often takes an hour or more.
tt := ds.Table(parts[2] + "_" + strings.Join(strings.Split(parts[3], "/"), ""))
err = WaitForStableTable(tt, dh.TestMode)
err = WaitForStableTable(tt)
if err != nil {
metrics.FailCount.WithLabelValues("WaitForStableTable")
task.SetError(err, "WaitForStableTable")
Expand Down
7 changes: 3 additions & 4 deletions dispatch/deduphandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ func TestDedupHandler(t *testing.T) {
client, counter := cloud.DryRunClient()

config := cloud.Config{
Project: "mlab-testing",
Client: client,
Options: []option.ClientOption{option.WithHTTPClient(client)},
TestMode: true}
Project: "mlab-testing",
Client: client,
Options: []option.ClientOption{option.WithHTTPClient(client)}}
bqConfig := cloud.BQConfig{
Config: config,
BQProject: "mlab-testing",
Expand Down
6 changes: 2 additions & 4 deletions dispatch/dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ func TestDispatcherLifeCycle(t *testing.T) {
// Use a fake client so we intercept all the http ops.
client, counter := cloud.DryRunClient()
config := cloud.Config{
Project: "mlab-testing",
Client: client,
Options: nil,
TestMode: true}
Project: "mlab-testing",
Client: client}
saver := S{tasks: make(map[string][]state.Task)}

// With time.Now(), this shouldn't send any requests.
Expand Down

0 comments on commit f8f686e

Please sign in to comment.