Shorten client/record test by changing default value

Also add opinionated comment about using singletons in code
pull/6/head
Clayton Coleman 2015-01-09 13:02:48 -05:00
parent dcd11761e7
commit cacd2ac40b
2 changed files with 18 additions and 10 deletions

View File

@ -30,6 +30,9 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
) )
// retryEventSleep is the time between record failures to retry. Available for test alteration.
var retryEventSleep = 1 * time.Second
// EventRecorder knows how to store events (client.Client implements it.) // EventRecorder knows how to store events (client.Client implements it.)
// EventRecorder must respect the namespace that will be embedded in 'event'. // EventRecorder must respect the namespace that will be embedded in 'event'.
// It is assumed that EventRecorder will return the same sorts of errors as // It is assumed that EventRecorder will return the same sorts of errors as
@ -41,6 +44,7 @@ type EventRecorder interface {
// StartRecording starts sending events to recorder. Call once while initializing // StartRecording starts sending events to recorder. Call once while initializing
// your binary. Subsequent calls will be ignored. The return value can be ignored // your binary. Subsequent calls will be ignored. The return value can be ignored
// or used to stop recording, if desired. // or used to stop recording, if desired.
// TODO: make me an object with parameterizable queue length and retry interval
func StartRecording(recorder EventRecorder, source api.EventSource) watch.Interface { func StartRecording(recorder EventRecorder, source api.EventSource) watch.Interface {
return GetEvents(func(event *api.Event) { return GetEvents(func(event *api.Event) {
// Make a copy before modification, because there could be multiple listeners. // Make a copy before modification, because there could be multiple listeners.
@ -80,7 +84,7 @@ func StartRecording(recorder EventRecorder, source api.EventSource) watch.Interf
break break
} }
glog.Errorf("Unable to write event: '%v' (will retry in 1 second)", err) glog.Errorf("Unable to write event: '%v' (will retry in 1 second)", err)
time.Sleep(1 * time.Second) time.Sleep(retryEventSleep)
} }
}) })
} }

View File

@ -14,22 +14,26 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package record_test package record
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
func init() {
retryEventSleep = 1 * time.Microsecond
}
type testEventRecorder struct { type testEventRecorder struct {
OnEvent func(e *api.Event) (*api.Event, error) OnEvent func(e *api.Event) (*api.Event, error)
} }
@ -142,16 +146,16 @@ func TestEventf(t *testing.T) {
return event, nil return event, nil
}, },
} }
recorder := record.StartRecording(&testEvents, api.EventSource{Component: "eventTest"}) recorder := StartRecording(&testEvents, api.EventSource{Component: "eventTest"})
logger := record.StartLogging(t.Logf) // Prove that it is useful logger := StartLogging(t.Logf) // Prove that it is useful
logger2 := record.StartLogging(func(formatter string, args ...interface{}) { logger2 := StartLogging(func(formatter string, args ...interface{}) {
if e, a := item.expectLog, fmt.Sprintf(formatter, args...); e != a { if e, a := item.expectLog, fmt.Sprintf(formatter, args...); e != a {
t.Errorf("Expected '%v', got '%v'", e, a) t.Errorf("Expected '%v', got '%v'", e, a)
} }
called <- struct{}{} called <- struct{}{}
}) })
record.Eventf(item.obj, item.status, item.reason, item.messageFmt, item.elements...) Eventf(item.obj, item.status, item.reason, item.messageFmt, item.elements...)
<-called <-called
<-called <-called
@ -204,7 +208,7 @@ func TestWriteEventError(t *testing.T) {
} }
done := make(chan struct{}) done := make(chan struct{})
defer record.StartRecording( defer StartRecording(
&testEventRecorder{ &testEventRecorder{
OnEvent: func(event *api.Event) (*api.Event, error) { OnEvent: func(event *api.Event) (*api.Event, error) {
if event.Message == "finished" { if event.Message == "finished" {
@ -227,9 +231,9 @@ func TestWriteEventError(t *testing.T) {
).Stop() ).Stop()
for caseName := range table { for caseName := range table {
record.Event(ref, "Status", "Reason", caseName) Event(ref, "Status", "Reason", caseName)
} }
record.Event(ref, "Status", "Reason", "finished") Event(ref, "Status", "Reason", "finished")
<-done <-done
for caseName, item := range table { for caseName, item := range table {