Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"testing"
"time"
)
const (
numInt10 = 10
)
func TestNewSendStats(t *testing.T) {
stats := NewSendStats(numInt10)
if stats.recordLen != numInt10 {
t.Errorf("Expected record length %d, but got %d", numInt10, stats.recordLen)
}
stats = NewSendStats(0)
if stats.recordLen != DefaultSendRecordLength {
t.Errorf("Expected default record length %d, but got %d",
DefaultSendRecordLength, stats.recordLen)
}
stats = NewSendStats(MaxSendRecordLength + 1)
if stats.recordLen != DefaultSendRecordLength {
t.Errorf("Expected default record length %d, but got %d",
DefaultSendRecordLength, stats.recordLen)
}
}
func TestRecordSendResult(t *testing.T) {
stats := NewSendStats(numInt10)
stats.RecordSendResult(true)
if len(stats.sendResults) != 0 {
t.Errorf("Expected sendResults length 0, but got %d", len(stats.sendResults))
}
for i := 0; i <= numInt10; i++ {
stats.RecordSendResult(false)
}
if len(stats.sendResults) != numInt10 {
t.Errorf("Expected sendResults length not to exceed record length %d, but got %d",
numInt10, len(stats.sendResults))
}
}
func TestGetConsecutiveFailures(t *testing.T) {
stats := NewSendStats(numInt10)
for i := 0; i < numInt10; i++ {
stats.RecordSendResult(true)
}
for i := 0; i < numInt10; i++ {
stats.RecordSendResult(false)
}
count := stats.GetConsecutiveFailures()
if count != numInt10 {
t.Errorf("Expected consecutive failures count %d, but got %d", numInt10, count)
}
successStats := &SendStats{
sendResults: []sendResult{
{
sendTime: time.Now(),
success: true,
},
},
recordLen: numInt10}
count = successStats.GetConsecutiveFailures()
if count != 0 {
t.Errorf("Expected consecutive failures count 0 when no failures, but got %d", count)
}
}
func TestGetLastSendStatus(t *testing.T) {
stats := NewSendStats(numInt10)
stats.RecordSendResult(true)
status := stats.GetLastSendStatus()
if !status {
t.Errorf("Expected last send status to be true, but got false")
}
stats.RecordSendResult(false)
status = stats.GetLastSendStatus()
if status {
t.Errorf("Expected last send status to be false, but got true")
}
emptyStats := NewSendStats(numInt10)
status = emptyStats.GetLastSendStatus()
if !status {
t.Errorf("Expected last send status to be true when no results, but got false")
}
}
func TestString(t *testing.T) {
stats := NewSendStats(numInt10)
for i := 0; i < numInt10; i++ {
stats.RecordSendResult(true)
}
str := stats.String()
if str == "" {
t.Errorf("Expected non - empty string representation, but got empty string")
}
}