Пример #1
0
func TestProducerMessageLoop(t *testing.T) {
	expect := shared.NewExpect(t)
	mockP := getMockProducer()
	mockP.setState(PluginStateActive)
	mockP.messages = make(chan Message, 10)
	msgData := "test Message loop"
	msg := Message{
		Data: []byte(msgData),
	}

	for i := 0; i < 9; i++ {
		mockP.messages <- msg
	}
	counter := 0
	onMessage := func(msg Message) {
		expect.Equal(msgData, msg.String())
		counter++
		if counter == 9 {
			mockP.setState(PluginStateDead)
		}
	}

	mockP.messageLoop(onMessage)
	expect.Equal(9, counter)
}
Пример #2
0
func TestPluginRunState(t *testing.T) {
	expect := shared.NewExpect(t)
	pluginState := NewPluginRunState()

	expect.Equal(PluginStateDead, pluginState.GetState())

	pluginState.SetState(PluginStateWaiting)
	expect.Equal(PluginStateWaiting, pluginState.GetState())
	pluginState.SetState(PluginStateActive)
	expect.Equal(PluginStateActive, pluginState.GetState())
	pluginState.SetState(PluginStateStopping)
	expect.Equal(PluginStateStopping, pluginState.GetState())

	var wg sync.WaitGroup
	pluginState.SetWorkerWaitGroup(&wg)

	pluginState.AddWorker()
	pluginState.AddWorker()
	done := false

	go func() {
		pluginState.WorkerDone()
		pluginState.WorkerDone()
		wg.Wait()
		done = true
	}()
	// timeout for go routine.
	time.Sleep(500 * time.Millisecond)
	expect.True(done)

}
Пример #3
0
// Function reads initializes pluginConfig with predefined values and
// non-predefined values in the Settings
// Plan:
//  Create a new PluginConfig
//  Create a new shared.MarshalMap with mock key values
//  Check if they are actually added, if they are, assert the key-value correctness
func TestPluginConfigRead(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	// create a mock MarshalMap
	testMarshalMap := shared.NewMarshalMap()
	testMarshalMap["Instances"] = 0

	mockPluginCfg.Read(testMarshalMap)
	// with 0 instance, plugin should be disabled
	expect.False(mockPluginCfg.Enable)
	// if there is no stream, then array with wildcard should be returned
	expect.Equal(mockPluginCfg.Stream, []string{"*"})

	//reset mockPluginCfg
	mockPluginCfg = getMockPluginConfig()
	testMarshalMap["ID"] = "mockId"
	testMarshalMap["Enable"] = true
	testMarshalMap["Instances"] = 2
	testMarshalMap["Stream"] = "mockStats"
	testMarshalMap["Host"] = "someHost"
	testMarshalMap["Database"] = "someDatabase"

	mockPluginCfg.Read(testMarshalMap)

	// Check for the bundled config options
	expect.Equal(mockPluginCfg.ID, "mockId")
	expect.True(mockPluginCfg.Enable)
	expect.Equal(mockPluginCfg.Instances, 2)
	expect.Equal(mockPluginCfg.Stream, []string{"mockStats"})

	// check for the miscelleneous settings key added
	expect.Equal(mockPluginCfg.GetString("Host", ""), "someHost")
	expect.Equal(mockPluginCfg.GetString("Database", ""), "someDatabase")
}
Пример #4
0
// For completeness' sake. This is exactly the same as Producer's ticket loop
func TestConsumerTickerLoop(t *testing.T) {
	expect := shared.NewExpect(t)
	mockC := getMockConsumer()
	mockC.setState(PluginStateActive)
	// accept timeroff by abs( 8 ms)
	delta := float64(8 * time.Millisecond)
	var counter = 0
	tickerLoopTimeout := 20 * time.Millisecond
	var timeRecorded time.Time
	onTimeOut := func() {
		if counter > 3 {
			mockC.setState(PluginStateDead)
			return
		}
		//this was fired as soon as the ticker started. So ignore but save the time
		if counter == 0 {
			timeRecorded = time.Now()
			counter++
			return
		}
		diff := time.Now().Sub(timeRecorded)
		deltaDiff := math.Abs(float64(tickerLoopTimeout - diff))
		expect.True(deltaDiff < delta)
		timeRecorded = time.Now()
		counter++
		return
	}

	mockC.tickerLoop(tickerLoopTimeout, onTimeOut)
	time.Sleep(2 * time.Second)
	// in anycase, the callback has to be called atleast once
	expect.True(counter > 1)
}
Пример #5
0
// For completeness' sake. This is exactly the same as Producer's control loop
func TestConsumerControlLoop(t *testing.T) {
	expect := shared.NewExpect(t)
	mockC := getMockConsumer()

	var stop bool
	var roll bool
	mockC.SetStopCallback(func() {
		stop = true
	})

	mockC.SetRollCallback(func() {
		roll = true
	})

	go mockC.ControlLoop()
	// let the go routine start
	time.Sleep(50 * time.Millisecond)
	mockC.control <- PluginControlStopConsumer
	time.Sleep(50 * time.Millisecond)
	expect.True(stop)

	go mockC.ControlLoop()
	time.Sleep(50 * time.Millisecond)
	mockC.control <- PluginControlRoll
	time.Sleep(50 * time.Millisecond)
	expect.True(roll)
}
Пример #6
0
func TestProducerControlLoop(t *testing.T) {
	expect := shared.NewExpect(t)
	mockP := getMockProducer()

	var stop bool
	var roll bool
	mockP.onStop = func() {
		stop = true
	}

	mockP.onRoll = func() {
		roll = true
	}

	go expect.NonBlocking(2*time.Second, mockP.ControlLoop)
	time.Sleep(50 * time.Millisecond)
	mockP.control <- PluginControlStopProducer // trigger stopLoop (stop expect.NonBlocking)
	time.Sleep(50 * time.Millisecond)
	expect.True(stop)

	go expect.NonBlocking(2*time.Second, mockP.ControlLoop)
	time.Sleep(50 * time.Millisecond)
	mockP.control <- PluginControlRoll // trigger rollLoop (stop expect.NonBlocking)
	time.Sleep(50 * time.Millisecond)
	expect.True(roll)

}
Пример #7
0
func TestConsumerEnqueueCopy(t *testing.T) {
	expect := shared.NewExpect(t)
	mockC := getMockConsumer()

	dataToSend := "Consumer Enqueue Data"
	distribute := func(msg Message) {
		expect.Equal(dataToSend, msg.String())
	}

	mockStream := getMockStream()
	mockP := getMockProducer()
	mockStream.AddProducer(&mockP)
	mockStream.distribute = distribute
	mockStreamID := GetStreamID("mockStream")
	StreamRegistry.Register(&mockStream, mockStreamID)

	mockC.streams = []MappedStream{
		MappedStream{
			StreamID: mockStreamID,
			Stream:   &mockStream,
		},
	}

	mockC.EnqueueCopy([]byte(dataToSend), 2)

}
Пример #8
0
func TestJSONFormatter1(t *testing.T) {
	expect := shared.NewExpect(t)

	testString := `{"a":123,"b":"string","c":[1,2,3],"d":[{"a":1}],"e":[[1,2]],"f":[{"a":1},{"b":2}],"g":[[1,2],[3,4]]}`
	msg := core.NewMessage(nil, []byte(testString), 0)
	test := newTestJSONFormatter([]interface{}{
		`findKey    :":  key        ::`,
		`findKey    :}:             : pop  : end`,
		`key        :":  findVal    :      : key`,
		`findVal    :\:: value      ::`,
		`value      :":  string     ::`,
		`value      :[:  array      : push : arr`,
		`value      :{:  findKey    : push : obj`,
		`value      :,:  findKey    :      : val`,
		`value      :}:             : pop  : val+end`,
		`string     :":  findKey    :      : esc`,
		`array      :[:  array      : push : arr`,
		`array      :{:  findKey    : push : obj`,
		`array      :]:             : pop  : end`,
		`array      :,:  arrIntVal  :      : val`,
		`array      :":  arrStrVal  ::`,
		`arrIntVal  :,:  arrIntVal  :      : val`,
		`arrIntVal  :]:             : pop  : val+end`,
		`arrStrVal  :":  arrNextStr :      : esc`,
		`arrNextStr :":  arrStrVal  ::`,
		`arrNextStr :]:             : pop  : end`,
	}, "findKey")

	result, _ := test.Format(msg)
	expect.Equal(testString, string(result))
}
Пример #9
0
// Function gets an bool value for a key or default if non-existant
// Plan: similar to TestPluginConfigGetInt
func TestPluginConfigGetBool(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	expect.Equal(mockPluginCfg.GetBool("boolKey", false), false)
	mockPluginCfg.Settings["boolKey"] = true
	expect.Equal(mockPluginCfg.GetBool("boolKey", false), true)
}
Пример #10
0
func TestWriterAssemblySetWriter(t *testing.T) {
	expect := shared.NewExpect(t)
	mockIo := mockIoWrite{expect}
	wa := NewWriterAssembly(mockIo, mockIo.mockFlush, &mockFormatter{})

	wa.SetWriter(secondMockIoWrite{})
	length, _ := wa.writer.Write([]byte("abcde"))
	expect.Equal(length, 5)
}
Пример #11
0
func TestConsumerFuse(t *testing.T) {
	expect := shared.NewExpect(t)
	mockC := getMockConsumer()

	conf := getMockPluginConfig()
	conf.Override("Fuse", "test")

	mockC.Configure(conf)
	expect.NotNil(mockC.fuse)

	burnedCallback := false
	activeCallback := false

	mockC.SetFuseBurnedCallback(func() { burnedCallback = true })
	mockC.SetFuseActiveCallback(func() { activeCallback = true })

	go mockC.ControlLoop()

	expect.False(mockC.fuse.IsBurned())
	expect.False(burnedCallback)
	expect.False(activeCallback)

	// Check manual fuse trigger

	burnedCallback = false
	activeCallback = false

	mockC.Control() <- PluginControlFuseBurn
	time.Sleep(10 * time.Millisecond)
	expect.True(burnedCallback)
	expect.False(activeCallback)

	burnedCallback = false
	mockC.Control() <- PluginControlFuseActive
	time.Sleep(10 * time.Millisecond)
	expect.False(burnedCallback)
	expect.True(activeCallback)

	// Check automatic burn callback

	burnedCallback = false
	activeCallback = false

	mockC.fuse.Burn()
	time.Sleep(shared.SpinTimeSuspend + 100*time.Millisecond)

	expect.True(burnedCallback)
	expect.False(activeCallback)

	// Check automatic activate callback

	burnedCallback = false
	mockC.fuse.Activate()
	time.Sleep(10 * time.Millisecond)
	expect.False(burnedCallback)
	expect.True(activeCallback)
}
Пример #12
0
// Function checks if predefined value exists or not
// Plan:
//  Create a new PluginConfig
//  Add non-predefined Values
//  Check if added ones return true, and others false
func TestPluginConfigHasValue(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	expect.False(mockPluginCfg.HasValue("aKey"))

	mockPluginCfg.Settings["aKey"] = 1
	expect.True(mockPluginCfg.HasValue("aKey"))
}
Пример #13
0
// Function gets an int value for a key or default value if non-existant
// Plan:
//  Create a new PluginConfig
//  add a key and an int value in the Settings
//  get the value back and Assert
func TestPluginConfigGetInt(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	expect.Equal(mockPluginCfg.GetInt("intKey", 0), 0)
	mockPluginCfg.Settings["intKey"] = 2
	expect.Equal(mockPluginCfg.GetInt("intKey", 0), 2)

}
Пример #14
0
func TestWriterAssemblySetValidator(t *testing.T) {
	expect := shared.NewExpect(t)
	mockIo := mockIoWrite{expect}
	wa := NewWriterAssembly(mockIo, mockIo.mockFlush, &mockFormatter{})
	validator := func() bool {
		return true
	}
	wa.SetValidator(validator)
	expect.True(wa.validate())
}
Пример #15
0
func TestStreamRegistryRegister(t *testing.T) {
	expect := shared.NewExpect(t)
	mockSRegistry := getMockStreamRegistry()

	streamName := "testStream"
	mockStream := getMockStream()
	mockSRegistry.Register(&mockStream, GetStreamID(streamName))

	expect.NotNil(mockSRegistry.GetStream(GetStreamID(streamName)))
}
Пример #16
0
// Function gets the string value for a key or if the key/value(?) doesn't
// exist, returns the default value
// Plan:
//  Create a new PluginConfig
//  For a random key, check if the default value is returned
//  Add a key with a value
//  Asserts the string returned for the key is correct
func TestPluginConfigGetString(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	//check for non-existant key
	expect.Equal(mockPluginCfg.GetString("aKey", "default"), "default")

	mockPluginCfg.Settings["aKey"] = "aValue"
	expect.Equal(mockPluginCfg.GetString("aKey", "default"), "aValue")
}
Пример #17
0
func TestStreamRegistryIsStreamRegistered(t *testing.T) {
	expect := shared.NewExpect(t)
	mockSRegistry := getMockStreamRegistry()

	mockStreamID := GetStreamID("testStream")

	expect.False(mockSRegistry.IsStreamRegistered(mockStreamID))
	// TODO: Get a real stream and test with that
	mockSRegistry.streams[mockStreamID] = &StreamBase{}
	expect.True(mockSRegistry.IsStreamRegistered(mockStreamID))
}
Пример #18
0
func TestStreamRegistryForEachStream(t *testing.T) {
	expect := shared.NewExpect(t)
	mockSRegistry := getMockStreamRegistry()

	callback := func(streamID MessageStreamID, stream Stream) {
		expect.Equal(streamID, GetStreamID("testRegistry"))
	}

	mockSRegistry.streams[GetStreamID("testRegistry")] = &StreamBase{}
	mockSRegistry.ForEachStream(callback)
}
Пример #19
0
func TestStreamRegistryGetStreamByName(t *testing.T) {
	expect := shared.NewExpect(t)
	mockSRegistry := getMockStreamRegistry()

	streamName := mockSRegistry.GetStreamByName("testStream")
	expect.Equal(streamName, nil)

	mockStreamID := GetStreamID("testStream")
	// TODO: Get a real stream and test with that
	mockSRegistry.streams[mockStreamID] = &StreamBase{}
	expect.Equal(mockSRegistry.GetStreamByName("testStream"), &StreamBase{})
}
Пример #20
0
func TestConsumerControl(t *testing.T) {
	expect := shared.NewExpect(t)
	mockC := getMockConsumer()
	mockC.control = make(chan PluginControl, 2)

	ctrlChan := mockC.Control()
	ctrlChan <- PluginControlRoll
	ctrlChan <- PluginControlStopConsumer

	expect.Equal(PluginControlRoll, <-mockC.control)
	expect.Equal(PluginControlStopConsumer, <-mockC.control)
}
Пример #21
0
func TestStreamRegistryWildcardProducer(t *testing.T) {
	expect := shared.NewExpect(t)
	mockSRegistry := getMockStreamRegistry()
	// WildcardProducersExist()
	expect.False(mockSRegistry.WildcardProducersExist())

	producer1 := new(mockProducer)
	producer2 := new(mockProducer)

	mockSRegistry.RegisterWildcardProducer(producer1, producer2)

	expect.True(mockSRegistry.WildcardProducersExist())
}
Пример #22
0
func TestWriterAssemblySetErrorHandler(t *testing.T) {
	expect := shared.NewExpect(t)
	mockIo := mockIoWrite{expect}
	wa := NewWriterAssembly(mockIo, mockIo.mockFlush, &mockFormatter{})
	handler := func(e error) bool {
		if e.Error() == "abc" {
			return true
		}
		return false
	}

	wa.SetErrorHandler(handler)
	expect.True(wa.handleError(errors.New("abc")))
}
Пример #23
0
func TestStreamRegistryGetStreamName(t *testing.T) {
	expect := shared.NewExpect(t)
	mockSRegistry := getMockStreamRegistry()

	expect.Equal(mockSRegistry.GetStreamName(DroppedStreamID), DroppedStream)
	expect.Equal(mockSRegistry.GetStreamName(LogInternalStreamID), LogInternalStream)
	expect.Equal(mockSRegistry.GetStreamName(WildcardStreamID), WildcardStream)

	mockStreamID := GetStreamID("test")
	expect.Equal(mockSRegistry.GetStreamName(mockStreamID), "")

	mockSRegistry.name[mockStreamID] = "test"
	expect.Equal(mockSRegistry.GetStreamName(mockStreamID), "test")
}
Пример #24
0
// Function gets the string array for a key or default value if non existant
// Plan: Similart to TestPluginConfigGetString
func TestPluginConfigGetStringArray(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	mockStringArray := []string{"el1", "el2", "el3"}

	// default value is returned
	expect.Equal(len(mockPluginCfg.GetStringArray("arrKey", []string{})), 0)

	mockPluginCfg.Settings["arrKey"] = mockStringArray
	//since expect.Equal is doing reflect.deepValueEqual, arrays should be properly compared
	expect.Equal(mockPluginCfg.GetStringArray("arrKey", []string{}), mockStringArray)

}
Пример #25
0
// Function gets a value for a key which is neither int or bool. Value encapsulated by interface
// Plan: similart to TestPluginConfigGetInt
func TestPluginConfigGetValue(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	// get string value
	expect.Equal(mockPluginCfg.GetValue("valStrKey", ""), "")
	mockPluginCfg.Settings["valStrKey"] = "valStr"
	expect.Equal(mockPluginCfg.GetValue("valStrKey", ""), "valStr")

	// get int value
	expect.Equal(mockPluginCfg.GetValue("valIntKey", 0), 0)
	mockPluginCfg.Settings["valIntKey"] = 1
	expect.Equal(mockPluginCfg.GetValue("valIntKey", 0), 1)

	// get bool value
	expect.Equal(mockPluginCfg.GetValue("valBoolKey", false), false)
	mockPluginCfg.Settings["valBoolKey"] = true
	expect.Equal(mockPluginCfg.GetValue("valBoolKey", false), true)

	// get a custom struct
	type CustomStruct struct {
		IntKey  int
		StrKey  string
		BoolKey bool
	}

	mockStruct := CustomStruct{1, "hello", true}
	//not sure if equal will do the trick here so, manual
	defaultCStruct := CustomStruct{0, "", false}
	defaultCStructRet := mockPluginCfg.GetValue("cStruct", defaultCStruct)
	ret, ok := defaultCStructRet.(CustomStruct)
	expect.True(ok)
	if ok {
		expect.Equal(defaultCStruct.IntKey, ret.IntKey)
		expect.Equal(defaultCStruct.BoolKey, ret.BoolKey)
		expect.Equal(defaultCStruct.StrKey, ret.StrKey)
	}

	mockPluginCfg.Settings["cStruct"] = mockStruct
	mockStructRet := mockPluginCfg.GetValue("cStruct", defaultCStruct)
	ret, ok = mockStructRet.(CustomStruct)
	expect.True(ok)
	if ok {
		expect.Equal(mockStruct.IntKey, ret.IntKey)
		expect.Equal(mockStruct.BoolKey, ret.BoolKey)
		expect.Equal(mockStruct.StrKey, ret.StrKey)
	}

}
Пример #26
0
// Function gets the stringMap for a key or default value if not existant
// Plan: Similar to TestPluginConfigGetString but the Map structure needs assertion
func TestPluginConfigGetStringMap(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()

	mockStringMap := map[string]string{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
	}

	expect.Equal(len(mockPluginCfg.GetStringMap("strMapKey", map[string]string{})), 0)

	mockPluginCfg.Settings["strMapKey"] = mockStringMap
	expect.Equal(mockPluginCfg.GetStringMap("strMapKey", map[string]string{}), mockStringMap)
}
Пример #27
0
func TestConsumerConfigure(t *testing.T) {
	expect := shared.NewExpect(t)
	mockC := getMockConsumer()

	pluginCfg := getMockPluginConfig()

	mockStream := getMockStream()
	mockStreamID := GetStreamID("mockStream")
	StreamRegistry.Register(&mockStream, mockStreamID)

	pluginCfg.Stream = []string{"mockStream"}

	err := mockC.Configure(pluginCfg)
	expect.Nil(err)
}
Пример #28
0
func TestWriterAssemblyWrite(t *testing.T) {
	expect := shared.NewExpect(t)
	mockIo := mockIoWrite{expect}
	wa := NewWriterAssembly(nil, mockIo.mockFlush, &mockFormatter{})

	msg1 := NewMessage(nil, []byte("abcde"), 0)

	// should give error msg and flush msg as ther writer is not available yet
	// test here is done in mockIo.mockFlush
	wa.Write([]Message{msg1})

	wa.SetWriter(mockIo)
	//this should extend the wa's buffer to 5 -> "abcde"
	wa.Write([]Message{msg1})
	//should override in the same buffer from beginning
	wa.Write([]Message{msg1})

	//set validator to return false so final flush is executed
	validator := func() bool {
		return false
	}
	wa.SetValidator(validator)
	wa.Write([]Message{msg1})
	// this writer object return error value
	wa.SetWriter(secondMockIoWrite{})
	wa.Write([]Message{msg1})

	// set errorHandler
	handlerReturnsTrue := func(e error) bool {
		if e.Error() == "someError" {
			return true
		}
		return false
	}
	wa.SetErrorHandler(handlerReturnsTrue)
	wa.Write([]Message{msg1})

	handlerWithoutError := func(e error) bool {
		if e.Error() == "someError" {
			return false
		}
		return true
	}
	wa.SetErrorHandler(handlerWithoutError)
	wa.Write([]Message{msg1})

}
Пример #29
0
func TestStreamRegistryGetStreamOrFallback(t *testing.T) {
	expect := shared.NewExpect(t)
	mockSRegistry := getMockStreamRegistry()

	expect.Equal(len(mockSRegistry.streams), 0)
	expect.Equal(len(mockSRegistry.wildcard), 0)

	streamName := "testStream"
	streamID := GetStreamID(streamName)
	mockSRegistry.GetStreamOrFallback(streamID)

	expect.Equal(len(mockSRegistry.streams), 1)

	// try registering again. No new register should happen.
	mockSRegistry.GetStreamOrFallback(streamID)
	expect.Equal(len(mockSRegistry.streams), 1)
}
Пример #30
0
// Function checks if non-predefined exists and has been accessed or not
// Plan:
//  Create a new PluginConfig
//  Access few keys
//  check True for existing & accessed keys and false otherwise
//
func TestPluginConfigValidate(t *testing.T) {
	expect := shared.NewExpect(t)
	mockPluginCfg := getMockPluginConfig()
	mockPluginCfg.Settings["stringKey"] = "value"
	mockPluginCfg.Settings["number"] = 1

	// access one field
	sValue := mockPluginCfg.GetString("stringKey", "")
	expect.Equal(sValue, "value")
	expect.False(mockPluginCfg.Validate())

	// access second one
	iValue := mockPluginCfg.GetInt("number", 0)
	expect.Equal(iValue, 1)
	expect.True(mockPluginCfg.Validate())

}