How to use Times method of got Package

Best Got code snippet using got.Times

encoding_test.go

Source:encoding_test.go Github

copy

Full Screen

...9	"github.com/influxdata/influxdb/tsdb/engine/tsm1"10)11func TestEncoding_FloatBlock(t *testing.T) {12	valueCount := 100013	times := getTimes(valueCount, 60, time.Second)14	values := make([]tsm1.Value, len(times))15	for i, t := range times {16		values[i] = tsm1.NewValue(t, float64(i))17	}18	b, err := tsm1.Values(values).Encode(nil)19	if err != nil {20		t.Fatalf("unexpected error: %v", err)21	}22	var decodedValues []tsm1.Value23	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)24	if err != nil {25		t.Fatalf("unexpected error decoding block: %v", err)26	}27	if !reflect.DeepEqual(decodedValues, values) {28		t.Fatalf("unexpected results:\n\tgot: %s\n\texp: %s\n", spew.Sdump(decodedValues), spew.Sdump(values))29	}30}31func TestEncoding_FloatBlock_ZeroTime(t *testing.T) {32	values := make([]tsm1.Value, 3)33	for i := 0; i < 3; i++ {34		values[i] = tsm1.NewValue(0, float64(i))35	}36	b, err := tsm1.Values(values).Encode(nil)37	if err != nil {38		t.Fatalf("unexpected error: %v", err)39	}40	var decodedValues []tsm1.Value41	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)42	if err != nil {43		t.Fatalf("unexpected error decoding block: %v", err)44	}45	if !reflect.DeepEqual(decodedValues, values) {46		t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)47	}48}49func TestEncoding_FloatBlock_SimilarFloats(t *testing.T) {50	values := make([]tsm1.Value, 5)51	values[0] = tsm1.NewValue(1444238178437870000, 6.00065e+06)52	values[1] = tsm1.NewValue(1444238185286830000, 6.000656e+06)53	values[2] = tsm1.NewValue(1444238188441501000, 6.000657e+06)54	values[3] = tsm1.NewValue(1444238195286811000, 6.000659e+06)55	values[4] = tsm1.NewValue(1444238198439917000, 6.000661e+06)56	b, err := tsm1.Values(values).Encode(nil)57	if err != nil {58		t.Fatalf("unexpected error: %v", err)59	}60	var decodedValues []tsm1.Value61	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)62	if err != nil {63		t.Fatalf("unexpected error decoding block: %v", err)64	}65	if !reflect.DeepEqual(decodedValues, values) {66		t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)67	}68}69func TestEncoding_IntBlock_Basic(t *testing.T) {70	valueCount := 100071	times := getTimes(valueCount, 60, time.Second)72	values := make([]tsm1.Value, len(times))73	for i, t := range times {74		values[i] = tsm1.NewValue(t, int64(i))75	}76	b, err := tsm1.Values(values).Encode(nil)77	if err != nil {78		t.Fatalf("unexpected error: %v", err)79	}80	var decodedValues []tsm1.Value81	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)82	if err != nil {83		t.Fatalf("unexpected error decoding block: %v", err)84	}85	if len(decodedValues) != len(values) {86		t.Fatalf("unexpected results length:\n\tgot: %v\n\texp: %v\n", len(decodedValues), len(values))87	}88	for i := 0; i < len(decodedValues); i++ {89		if decodedValues[i].UnixNano() != values[i].UnixNano() {90			t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues[i].UnixNano(), values[i].UnixNano())91		}92		if decodedValues[i].Value() != values[i].Value() {93			t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues[i].Value(), values[i].Value())94		}95	}96}97func TestEncoding_IntBlock_Negatives(t *testing.T) {98	valueCount := 100099	times := getTimes(valueCount, 60, time.Second)100	values := make([]tsm1.Value, len(times))101	for i, t := range times {102		v := int64(i)103		if i%2 == 0 {104			v = -v105		}106		values[i] = tsm1.NewValue(t, int64(v))107	}108	b, err := tsm1.Values(values).Encode(nil)109	if err != nil {110		t.Fatalf("unexpected error: %v", err)111	}112	var decodedValues []tsm1.Value113	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)114	if err != nil {115		t.Fatalf("unexpected error decoding block: %v", err)116	}117	if !reflect.DeepEqual(decodedValues, values) {118		t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)119	}120}121func TestEncoding_UIntBlock_Basic(t *testing.T) {122	valueCount := 1000123	times := getTimes(valueCount, 60, time.Second)124	values := make([]tsm1.Value, len(times))125	for i, t := range times {126		values[i] = tsm1.NewValue(t, uint64(i))127	}128	b, err := tsm1.Values(values).Encode(nil)129	if err != nil {130		t.Fatalf("unexpected error: %v", err)131	}132	var decodedValues []tsm1.Value133	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)134	if err != nil {135		t.Fatalf("unexpected error decoding block: %v", err)136	}137	if len(decodedValues) != len(values) {138		t.Fatalf("unexpected results length:\n\tgot: %v\n\texp: %v\n", len(decodedValues), len(values))139	}140	for i := 0; i < len(decodedValues); i++ {141		if decodedValues[i].UnixNano() != values[i].UnixNano() {142			t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues[i].UnixNano(), values[i].UnixNano())143		}144		if decodedValues[i].Value() != values[i].Value() {145			t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues[i].Value(), values[i].Value())146		}147	}148}149// TestEncoding_UIntBlock_MaxValues encodes uint64 numbers starting at max (18446744073709551615)150// down to 18446744073709550616151func TestEncoding_UIntBlock_MaxValues(t *testing.T) {152	valueCount := 1000153	times := getTimes(valueCount, 60, time.Second)154	values := make([]tsm1.Value, len(times))155	for i, t := range times {156		values[i] = tsm1.NewValue(t, ^uint64(i))157	}158	b, err := tsm1.Values(values).Encode(nil)159	if err != nil {160		t.Fatalf("unexpected error: %v", err)161	}162	var decodedValues []tsm1.Value163	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)164	if err != nil {165		t.Fatalf("unexpected error decoding block: %v", err)166	}167	if !reflect.DeepEqual(decodedValues, values) {168		t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)169	}170}171func TestEncoding_BooleanBlock_Basic(t *testing.T) {172	valueCount := 1000173	times := getTimes(valueCount, 60, time.Second)174	values := make([]tsm1.Value, len(times))175	for i, t := range times {176		v := true177		if i%2 == 0 {178			v = false179		}180		values[i] = tsm1.NewValue(t, v)181	}182	b, err := tsm1.Values(values).Encode(nil)183	if err != nil {184		t.Fatalf("unexpected error: %v", err)185	}186	var decodedValues []tsm1.Value187	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)188	if err != nil {189		t.Fatalf("unexpected error decoding block: %v", err)190	}191	if !reflect.DeepEqual(decodedValues, values) {192		t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)193	}194}195func TestEncoding_StringBlock_Basic(t *testing.T) {196	valueCount := 1000197	times := getTimes(valueCount, 60, time.Second)198	values := make([]tsm1.Value, len(times))199	for i, t := range times {200		values[i] = tsm1.NewValue(t, fmt.Sprintf("value %d", i))201	}202	b, err := tsm1.Values(values).Encode(nil)203	if err != nil {204		t.Fatalf("unexpected error: %v", err)205	}206	var decodedValues []tsm1.Value207	decodedValues, err = tsm1.DecodeBlock(b, decodedValues)208	if err != nil {209		t.Fatalf("unexpected error decoding block: %v", err)210	}211	if !reflect.DeepEqual(decodedValues, values) {212		t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)213	}214}215func TestEncoding_BlockType(t *testing.T) {216	tests := []struct {217		value     interface{}218		blockType byte219	}{220		{value: float64(1.0), blockType: tsm1.BlockFloat64},221		{value: int64(1), blockType: tsm1.BlockInteger},222		{value: uint64(1), blockType: tsm1.BlockUnsigned},223		{value: true, blockType: tsm1.BlockBoolean},224		{value: "string", blockType: tsm1.BlockString},225	}226	for _, test := range tests {227		var values []tsm1.Value228		values = append(values, tsm1.NewValue(0, test.value))229		b, err := tsm1.Values(values).Encode(nil)230		if err != nil {231			t.Fatalf("unexpected error: %v", err)232		}233		bt, err := tsm1.BlockType(b)234		if err != nil {235			t.Fatalf("unexpected error decoding block type: %v", err)236		}237		if got, exp := bt, test.blockType; got != exp {238			t.Fatalf("block type mismatch: got %v, exp %v", got, exp)239		}240	}241	_, err := tsm1.BlockType([]byte{10})242	if err == nil {243		t.Fatalf("expected error decoding block type, got nil")244	}245}246func TestEncoding_Count(t *testing.T) {247	tests := []struct {248		value     interface{}249		blockType byte250	}{251		{value: float64(1.0), blockType: tsm1.BlockFloat64},252		{value: int64(1), blockType: tsm1.BlockInteger},253		{value: uint64(1), blockType: tsm1.BlockUnsigned},254		{value: true, blockType: tsm1.BlockBoolean},255		{value: "string", blockType: tsm1.BlockString},256	}257	for _, test := range tests {258		var values []tsm1.Value259		values = append(values, tsm1.NewValue(0, test.value))260		b, err := tsm1.Values(values).Encode(nil)261		if err != nil {262			t.Fatalf("unexpected error: %v", err)263		}264		if got, exp := tsm1.BlockCount(b), 1; got != exp {265			t.Fatalf("block count mismatch: got %v, exp %v", got, exp)266		}267	}268}269func TestValues_MergeFloat(t *testing.T) {270	tests := []struct {271		a, b, exp []tsm1.Value272	}{273		{ // empty a274			a: []tsm1.Value{},275			b: []tsm1.Value{276				tsm1.NewValue(1, 1.2),277				tsm1.NewValue(2, 2.2),278			},279			exp: []tsm1.Value{280				tsm1.NewValue(1, 1.2),281				tsm1.NewValue(2, 2.2),282			},283		},284		{ // empty b285			a: []tsm1.Value{286				tsm1.NewValue(1, 1.1),287				tsm1.NewValue(2, 2.1),288			},289			b: []tsm1.Value{},290			exp: []tsm1.Value{291				tsm1.NewValue(1, 1.1),292				tsm1.NewValue(2, 2.1),293			},294		},295		{296			a: []tsm1.Value{297				tsm1.NewValue(0, 0.0),298				tsm1.NewValue(1, 1.1),299				tsm1.NewValue(2, 2.1),300			},301			b: []tsm1.Value{302				tsm1.NewValue(2, 2.2),303				tsm1.NewValue(2, 2.2), // duplicate data304			},305			exp: []tsm1.Value{306				tsm1.NewValue(0, 0.0),307				tsm1.NewValue(1, 1.1),308				tsm1.NewValue(2, 2.2),309			},310		},311		{312			a: []tsm1.Value{313				tsm1.NewValue(0, 0.0),314				tsm1.NewValue(1, 1.1),315				tsm1.NewValue(1, 1.1), // duplicate data316				tsm1.NewValue(2, 2.1),317			},318			b: []tsm1.Value{319				tsm1.NewValue(2, 2.2),320				tsm1.NewValue(2, 2.2), // duplicate data321			},322			exp: []tsm1.Value{323				tsm1.NewValue(0, 0.0),324				tsm1.NewValue(1, 1.1),325				tsm1.NewValue(2, 2.2),326			},327		},328		{329			a: []tsm1.Value{330				tsm1.NewValue(1, 1.1),331			},332			b: []tsm1.Value{333				tsm1.NewValue(0, 0.0),334				tsm1.NewValue(1, 1.2), // overwrites a335				tsm1.NewValue(2, 2.2),336				tsm1.NewValue(3, 3.2),337				tsm1.NewValue(4, 4.2),338			},339			exp: []tsm1.Value{340				tsm1.NewValue(0, 0.0),341				tsm1.NewValue(1, 1.2),342				tsm1.NewValue(2, 2.2),343				tsm1.NewValue(3, 3.2),344				tsm1.NewValue(4, 4.2),345			},346		},347		{348			a: []tsm1.Value{349				tsm1.NewValue(1, 1.1),350				tsm1.NewValue(2, 2.1),351				tsm1.NewValue(3, 3.1),352				tsm1.NewValue(4, 4.1),353			},354			b: []tsm1.Value{355				tsm1.NewValue(1, 1.2), // overwrites a356				tsm1.NewValue(2, 2.2), // overwrites a357			},358			exp: []tsm1.Value{359				tsm1.NewValue(1, 1.2),360				tsm1.NewValue(2, 2.2),361				tsm1.NewValue(3, 3.1),362				tsm1.NewValue(4, 4.1),363			},364		},365		{366			a: []tsm1.Value{367				tsm1.NewValue(1, 1.1),368				tsm1.NewValue(2, 2.1),369				tsm1.NewValue(3, 3.1),370				tsm1.NewValue(4, 4.1),371			},372			b: []tsm1.Value{373				tsm1.NewValue(1, 1.2), // overwrites a374				tsm1.NewValue(2, 2.2), // overwrites a375				tsm1.NewValue(3, 3.2),376				tsm1.NewValue(4, 4.2),377			},378			exp: []tsm1.Value{379				tsm1.NewValue(1, 1.2),380				tsm1.NewValue(2, 2.2),381				tsm1.NewValue(3, 3.2),382				tsm1.NewValue(4, 4.2),383			},384		},385		{386			a: []tsm1.Value{387				tsm1.NewValue(0, 0.0),388				tsm1.NewValue(1, 1.1),389				tsm1.NewValue(2, 2.1),390				tsm1.NewValue(3, 3.1),391				tsm1.NewValue(4, 4.1),392			},393			b: []tsm1.Value{394				tsm1.NewValue(0, 0.0),395				tsm1.NewValue(2, 2.2),396				tsm1.NewValue(4, 4.2),397			},398			exp: []tsm1.Value{399				tsm1.NewValue(0, 0.0),400				tsm1.NewValue(1, 1.1),401				tsm1.NewValue(2, 2.2),402				tsm1.NewValue(3, 3.1),403				tsm1.NewValue(4, 4.2),404			},405		},406		{407			a: []tsm1.Value{408				tsm1.NewValue(1462498658242869207, 0.0),409				tsm1.NewValue(1462498658288956853, 1.1),410			},411			b: []tsm1.Value{412				tsm1.NewValue(1462498658242870810, 0.0),413				tsm1.NewValue(1462498658262911238, 2.2),414				tsm1.NewValue(1462498658282415038, 4.2),415				tsm1.NewValue(1462498658282417760, 4.2),416			},417			exp: []tsm1.Value{418				tsm1.NewValue(1462498658242869207, 0.0),419				tsm1.NewValue(1462498658242870810, 0.0),420				tsm1.NewValue(1462498658262911238, 2.2),421				tsm1.NewValue(1462498658282415038, 4.2),422				tsm1.NewValue(1462498658282417760, 4.2),423				tsm1.NewValue(1462498658288956853, 1.1),424			},425		},426		{427			a: []tsm1.Value{428				tsm1.NewValue(4, 4.0),429				tsm1.NewValue(5, 5.0),430				tsm1.NewValue(6, 6.0),431			},432			b: []tsm1.Value{433				tsm1.NewValue(1, 1.0),434				tsm1.NewValue(2, 2.0),435				tsm1.NewValue(3, 3.0),436			},437			exp: []tsm1.Value{438				tsm1.NewValue(1, 1.0),439				tsm1.NewValue(2, 2.0),440				tsm1.NewValue(3, 3.0),441				tsm1.NewValue(4, 4.0),442				tsm1.NewValue(5, 5.0),443				tsm1.NewValue(6, 6.0),444			},445		},446		{447			a: []tsm1.Value{448				tsm1.NewValue(5, 5.0),449				tsm1.NewValue(6, 6.0),450			},451			b: []tsm1.Value{452				tsm1.NewValue(1, 1.0),453				tsm1.NewValue(2, 2.0),454				tsm1.NewValue(3, 3.0),455				tsm1.NewValue(4, 4.0),456				tsm1.NewValue(7, 7.0),457				tsm1.NewValue(8, 8.0),458			},459			exp: []tsm1.Value{460				tsm1.NewValue(1, 1.0),461				tsm1.NewValue(2, 2.0),462				tsm1.NewValue(3, 3.0),463				tsm1.NewValue(4, 4.0),464				tsm1.NewValue(5, 5.0),465				tsm1.NewValue(6, 6.0),466				tsm1.NewValue(7, 7.0),467				tsm1.NewValue(8, 8.0),468			},469		},470		{471			a: []tsm1.Value{472				tsm1.NewValue(1, 1.0),473				tsm1.NewValue(2, 2.0),474				tsm1.NewValue(3, 3.0),475			},476			b: []tsm1.Value{477				tsm1.NewValue(4, 4.0),478				tsm1.NewValue(5, 5.0),479				tsm1.NewValue(6, 6.0),480			},481			exp: []tsm1.Value{482				tsm1.NewValue(1, 1.0),483				tsm1.NewValue(2, 2.0),484				tsm1.NewValue(3, 3.0),485				tsm1.NewValue(4, 4.0),486				tsm1.NewValue(5, 5.0),487				tsm1.NewValue(6, 6.0),488			},489		},490	}491	for i, test := range tests {492		got := tsm1.Values(test.a).Merge(test.b)493		if exp, got := len(test.exp), len(got); exp != got {494			t.Fatalf("test(%d): value length mismatch: exp %v, got %v", i, exp, got)495		}496		dedup := tsm1.Values(append(test.a, test.b...)).Deduplicate()497		for i := range test.exp {498			if exp, got := test.exp[i].String(), got[i].String(); exp != got {499				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)500			}501			if exp, got := test.exp[i].String(), dedup[i].String(); exp != got {502				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)503			}504		}505	}506}507func TestIntegerValues_Merge(t *testing.T) {508	integerValue := func(t int64, f int64) tsm1.IntegerValue {509		return tsm1.NewValue(t, f).(tsm1.IntegerValue)510	}511	tests := []struct {512		a, b, exp []tsm1.IntegerValue513	}{514		{ // empty a515			a: []tsm1.IntegerValue{},516			b: []tsm1.IntegerValue{517				integerValue(1, 10),518				integerValue(2, 20),519			},520			exp: []tsm1.IntegerValue{521				integerValue(1, 10),522				integerValue(2, 20),523			},524		},525		{ // empty b526			a: []tsm1.IntegerValue{527				integerValue(1, 1),528				integerValue(2, 2),529			},530			b: []tsm1.IntegerValue{},531			exp: []tsm1.IntegerValue{532				integerValue(1, 1),533				integerValue(2, 2),534			},535		},536		{537			a: []tsm1.IntegerValue{538				integerValue(1, 1),539			},540			b: []tsm1.IntegerValue{541				integerValue(0, 0),542				integerValue(1, 10), // overwrites a543				integerValue(2, 20),544				integerValue(3, 30),545				integerValue(4, 40),546			},547			exp: []tsm1.IntegerValue{548				integerValue(0, 0),549				integerValue(1, 10),550				integerValue(2, 20),551				integerValue(3, 30),552				integerValue(4, 40),553			},554		},555		{556			a: []tsm1.IntegerValue{557				integerValue(1, 1),558				integerValue(2, 2),559				integerValue(3, 3),560				integerValue(4, 4),561			},562			b: []tsm1.IntegerValue{563				integerValue(1, 10), // overwrites a564				integerValue(2, 20), // overwrites a565			},566			exp: []tsm1.IntegerValue{567				integerValue(1, 10),568				integerValue(2, 20),569				integerValue(3, 3),570				integerValue(4, 4),571			},572		},573		{574			a: []tsm1.IntegerValue{575				integerValue(1, 1),576				integerValue(2, 2),577				integerValue(3, 3),578				integerValue(4, 4),579			},580			b: []tsm1.IntegerValue{581				integerValue(1, 10), // overwrites a582				integerValue(2, 20), // overwrites a583				integerValue(3, 30),584				integerValue(4, 40),585			},586			exp: []tsm1.IntegerValue{587				integerValue(1, 10),588				integerValue(2, 20),589				integerValue(3, 30),590				integerValue(4, 40),591			},592		},593		{594			a: []tsm1.IntegerValue{595				integerValue(0, 0),596				integerValue(1, 1),597				integerValue(2, 2),598				integerValue(3, 3),599				integerValue(4, 4),600			},601			b: []tsm1.IntegerValue{602				integerValue(0, 0),603				integerValue(2, 20),604				integerValue(4, 40),605			},606			exp: []tsm1.IntegerValue{607				integerValue(0, 0.0),608				integerValue(1, 1),609				integerValue(2, 20),610				integerValue(3, 3),611				integerValue(4, 40),612			},613		},614	}615	for i, test := range tests {616		if i != 2 {617			continue618		}619		got := tsm1.IntegerValues(test.a).Merge(test.b)620		if exp, got := len(test.exp), len(got); exp != got {621			t.Fatalf("test(%d): value length mismatch: exp %v, got %v", i, exp, got)622		}623		dedup := tsm1.IntegerValues(append(test.a, test.b...)).Deduplicate()624		for i := range test.exp {625			if exp, got := test.exp[i].String(), got[i].String(); exp != got {626				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)627			}628			if exp, got := test.exp[i].String(), dedup[i].String(); exp != got {629				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)630			}631		}632	}633}634func TestUnsignedValues_Merge(t *testing.T) {635	uintValue := func(t int64, f uint64) tsm1.UnsignedValue {636		return tsm1.NewValue(t, f).(tsm1.UnsignedValue)637	}638	tests := []struct {639		a, b, exp []tsm1.UnsignedValue640	}{641		{ // empty a642			a: []tsm1.UnsignedValue{},643			b: []tsm1.UnsignedValue{644				uintValue(1, 10),645				uintValue(2, 20),646			},647			exp: []tsm1.UnsignedValue{648				uintValue(1, 10),649				uintValue(2, 20),650			},651		},652		{ // empty b653			a: []tsm1.UnsignedValue{654				uintValue(1, 1),655				uintValue(2, 2),656			},657			b: []tsm1.UnsignedValue{},658			exp: []tsm1.UnsignedValue{659				uintValue(1, 1),660				uintValue(2, 2),661			},662		},663		{664			a: []tsm1.UnsignedValue{665				uintValue(1, 1),666			},667			b: []tsm1.UnsignedValue{668				uintValue(0, 0),669				uintValue(1, 10), // overwrites a670				uintValue(2, 20),671				uintValue(3, 30),672				uintValue(4, 40),673			},674			exp: []tsm1.UnsignedValue{675				uintValue(0, 0),676				uintValue(1, 10),677				uintValue(2, 20),678				uintValue(3, 30),679				uintValue(4, 40),680			},681		},682		{683			a: []tsm1.UnsignedValue{684				uintValue(1, 1),685				uintValue(2, 2),686				uintValue(3, 3),687				uintValue(4, 4),688			},689			b: []tsm1.UnsignedValue{690				uintValue(1, ^uint64(0)), // overwrites a691				uintValue(2, 20),         // overwrites a692			},693			exp: []tsm1.UnsignedValue{694				uintValue(1, ^uint64(0)),695				uintValue(2, 20),696				uintValue(3, 3),697				uintValue(4, 4),698			},699		},700		{701			a: []tsm1.UnsignedValue{702				uintValue(1, 1),703				uintValue(2, 2),704				uintValue(3, 3),705				uintValue(4, 4),706			},707			b: []tsm1.UnsignedValue{708				uintValue(1, 10), // overwrites a709				uintValue(2, 20), // overwrites a710				uintValue(3, 30),711				uintValue(4, 40),712			},713			exp: []tsm1.UnsignedValue{714				uintValue(1, 10),715				uintValue(2, 20),716				uintValue(3, 30),717				uintValue(4, 40),718			},719		},720		{721			a: []tsm1.UnsignedValue{722				uintValue(0, 0),723				uintValue(1, 1),724				uintValue(2, 2),725				uintValue(3, 3),726				uintValue(4, 4),727			},728			b: []tsm1.UnsignedValue{729				uintValue(0, 0),730				uintValue(2, 20),731				uintValue(4, 40),732			},733			exp: []tsm1.UnsignedValue{734				uintValue(0, 0.0),735				uintValue(1, 1),736				uintValue(2, 20),737				uintValue(3, 3),738				uintValue(4, 40),739			},740		},741	}742	for i, test := range tests {743		if i != 2 {744			continue745		}746		got := tsm1.UnsignedValues(test.a).Merge(test.b)747		if exp, got := len(test.exp), len(got); exp != got {748			t.Fatalf("test(%d): value length mismatch: exp %v, got %v", i, exp, got)749		}750		dedup := tsm1.UnsignedValues(append(test.a, test.b...)).Deduplicate()751		for i := range test.exp {752			if exp, got := test.exp[i].String(), got[i].String(); exp != got {753				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)754			}755			if exp, got := test.exp[i].String(), dedup[i].String(); exp != got {756				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)757			}758		}759	}760}761func TestFloatValues_Merge(t *testing.T) {762	floatValue := func(t int64, f float64) tsm1.FloatValue {763		return tsm1.NewValue(t, f).(tsm1.FloatValue)764	}765	tests := []struct {766		a, b, exp []tsm1.FloatValue767	}{768		{ // empty a769			a: []tsm1.FloatValue{},770			b: []tsm1.FloatValue{771				floatValue(1, 1.2),772				floatValue(2, 2.2),773			},774			exp: []tsm1.FloatValue{775				floatValue(1, 1.2),776				floatValue(2, 2.2),777			},778		},779		{ // empty b780			a: []tsm1.FloatValue{781				floatValue(1, 1.1),782				floatValue(2, 2.1),783			},784			b: []tsm1.FloatValue{},785			exp: []tsm1.FloatValue{786				floatValue(1, 1.1),787				floatValue(2, 2.1),788			},789		},790		{791			a: []tsm1.FloatValue{792				floatValue(1, 1.1),793			},794			b: []tsm1.FloatValue{795				floatValue(0, 0.0),796				floatValue(1, 1.2), // overwrites a797				floatValue(2, 2.2),798				floatValue(3, 3.2),799				floatValue(4, 4.2),800			},801			exp: []tsm1.FloatValue{802				floatValue(0, 0.0),803				floatValue(1, 1.2),804				floatValue(2, 2.2),805				floatValue(3, 3.2),806				floatValue(4, 4.2),807			},808		},809		{810			a: []tsm1.FloatValue{811				floatValue(1, 1.1),812				floatValue(2, 2.1),813				floatValue(3, 3.1),814				floatValue(4, 4.1),815			},816			b: []tsm1.FloatValue{817				floatValue(1, 1.2), // overwrites a818				floatValue(2, 2.2), // overwrites a819			},820			exp: []tsm1.FloatValue{821				floatValue(1, 1.2),822				floatValue(2, 2.2),823				floatValue(3, 3.1),824				floatValue(4, 4.1),825			},826		},827		{828			a: []tsm1.FloatValue{829				floatValue(1, 1.1),830				floatValue(2, 2.1),831				floatValue(3, 3.1),832				floatValue(4, 4.1),833			},834			b: []tsm1.FloatValue{835				floatValue(1, 1.2), // overwrites a836				floatValue(2, 2.2), // overwrites a837				floatValue(3, 3.2),838				floatValue(4, 4.2),839			},840			exp: []tsm1.FloatValue{841				floatValue(1, 1.2),842				floatValue(2, 2.2),843				floatValue(3, 3.2),844				floatValue(4, 4.2),845			},846		},847		{848			a: []tsm1.FloatValue{849				floatValue(0, 0.0),850				floatValue(1, 1.1),851				floatValue(2, 2.1),852				floatValue(3, 3.1),853				floatValue(4, 4.1),854			},855			b: []tsm1.FloatValue{856				floatValue(0, 0.0),857				floatValue(2, 2.2),858				floatValue(4, 4.2),859			},860			exp: []tsm1.FloatValue{861				floatValue(0, 0.0),862				floatValue(1, 1.1),863				floatValue(2, 2.2),864				floatValue(3, 3.1),865				floatValue(4, 4.2),866			},867		},868	}869	for i, test := range tests {870		got := tsm1.FloatValues(test.a).Merge(test.b)871		if exp, got := len(test.exp), len(got); exp != got {872			t.Fatalf("test(%d): value length mismatch: exp %v, got %v", i, exp, got)873		}874		dedup := tsm1.FloatValues(append(test.a, test.b...)).Deduplicate()875		for i := range test.exp {876			if exp, got := test.exp[i].String(), got[i].String(); exp != got {877				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)878			}879			if exp, got := test.exp[i].String(), dedup[i].String(); exp != got {880				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)881			}882		}883	}884}885func TestBooleanValues_Merge(t *testing.T) {886	booleanValue := func(t int64, f bool) tsm1.BooleanValue {887		return tsm1.NewValue(t, f).(tsm1.BooleanValue)888	}889	tests := []struct {890		a, b, exp []tsm1.BooleanValue891	}{892		{ // empty a893			a: []tsm1.BooleanValue{},894			b: []tsm1.BooleanValue{895				booleanValue(1, true),896				booleanValue(2, true),897			},898			exp: []tsm1.BooleanValue{899				booleanValue(1, true),900				booleanValue(2, true),901			},902		},903		{ // empty b904			a: []tsm1.BooleanValue{905				booleanValue(1, true),906				booleanValue(2, true),907			},908			b: []tsm1.BooleanValue{},909			exp: []tsm1.BooleanValue{910				booleanValue(1, true),911				booleanValue(2, true),912			},913		},914		{915			a: []tsm1.BooleanValue{916				booleanValue(1, true),917			},918			b: []tsm1.BooleanValue{919				booleanValue(0, false),920				booleanValue(1, false), // overwrites a921				booleanValue(2, false),922				booleanValue(3, false),923				booleanValue(4, false),924			},925			exp: []tsm1.BooleanValue{926				booleanValue(0, false),927				booleanValue(1, false),928				booleanValue(2, false),929				booleanValue(3, false),930				booleanValue(4, false),931			},932		},933		{934			a: []tsm1.BooleanValue{935				booleanValue(1, true),936				booleanValue(2, true),937				booleanValue(3, true),938				booleanValue(4, true),939			},940			b: []tsm1.BooleanValue{941				booleanValue(1, false), // overwrites a942				booleanValue(2, false), // overwrites a943			},944			exp: []tsm1.BooleanValue{945				booleanValue(1, false), // overwrites a946				booleanValue(2, false), // overwrites a947				booleanValue(3, true),948				booleanValue(4, true),949			},950		},951		{952			a: []tsm1.BooleanValue{953				booleanValue(1, true),954				booleanValue(2, true),955				booleanValue(3, true),956				booleanValue(4, true),957			},958			b: []tsm1.BooleanValue{959				booleanValue(1, false), // overwrites a960				booleanValue(2, false), // overwrites a961				booleanValue(3, false),962				booleanValue(4, false),963			},964			exp: []tsm1.BooleanValue{965				booleanValue(1, false),966				booleanValue(2, false),967				booleanValue(3, false),968				booleanValue(4, false),969			},970		},971		{972			a: []tsm1.BooleanValue{973				booleanValue(0, true),974				booleanValue(1, true),975				booleanValue(2, true),976				booleanValue(3, true),977				booleanValue(4, true),978			},979			b: []tsm1.BooleanValue{980				booleanValue(0, false),981				booleanValue(2, false),982				booleanValue(4, false),983			},984			exp: []tsm1.BooleanValue{985				booleanValue(0, false),986				booleanValue(1, true),987				booleanValue(2, false),988				booleanValue(3, true),989				booleanValue(4, false),990			},991		},992	}993	for i, test := range tests {994		got := tsm1.BooleanValues(test.a).Merge(test.b)995		if exp, got := len(test.exp), len(got); exp != got {996			t.Fatalf("test(%d): value length mismatch: exp %v, got %v", i, exp, got)997		}998		dedup := tsm1.BooleanValues(append(test.a, test.b...)).Deduplicate()999		for i := range test.exp {1000			if exp, got := test.exp[i].String(), got[i].String(); exp != got {1001				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)1002			}1003			if exp, got := test.exp[i].String(), dedup[i].String(); exp != got {1004				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)1005			}1006		}1007	}1008}1009func TestStringValues_Merge(t *testing.T) {1010	stringValue := func(t int64, f string) tsm1.StringValue {1011		return tsm1.NewValue(t, f).(tsm1.StringValue)1012	}1013	tests := []struct {1014		a, b, exp []tsm1.StringValue1015	}{1016		{ // empty a1017			a: []tsm1.StringValue{},1018			b: []tsm1.StringValue{1019				stringValue(1, "10"),1020				stringValue(2, "20"),1021			},1022			exp: []tsm1.StringValue{1023				stringValue(1, "10"),1024				stringValue(2, "20"),1025			},1026		},1027		{ // empty b1028			a: []tsm1.StringValue{1029				stringValue(1, "1"),1030				stringValue(2, "2"),1031			},1032			b: []tsm1.StringValue{},1033			exp: []tsm1.StringValue{1034				stringValue(1, "1"),1035				stringValue(2, "2"),1036			},1037		},1038		{1039			a: []tsm1.StringValue{1040				stringValue(1, "1"),1041			},1042			b: []tsm1.StringValue{1043				stringValue(0, "0"),1044				stringValue(1, "10"), // overwrites a1045				stringValue(2, "20"),1046				stringValue(3, "30"),1047				stringValue(4, "40"),1048			},1049			exp: []tsm1.StringValue{1050				stringValue(0, "0"),1051				stringValue(1, "10"),1052				stringValue(2, "20"),1053				stringValue(3, "30"),1054				stringValue(4, "40"),1055			},1056		},1057		{1058			a: []tsm1.StringValue{1059				stringValue(1, "1"),1060				stringValue(2, "2"),1061				stringValue(3, "3"),1062				stringValue(4, "4"),1063			},1064			b: []tsm1.StringValue{1065				stringValue(1, "10"), // overwrites a1066				stringValue(2, "20"), // overwrites a1067			},1068			exp: []tsm1.StringValue{1069				stringValue(1, "10"),1070				stringValue(2, "20"),1071				stringValue(3, "3"),1072				stringValue(4, "4"),1073			},1074		},1075		{1076			a: []tsm1.StringValue{1077				stringValue(1, "1"),1078				stringValue(2, "2"),1079				stringValue(3, "3"),1080				stringValue(4, "4"),1081			},1082			b: []tsm1.StringValue{1083				stringValue(1, "10"), // overwrites a1084				stringValue(2, "20"), // overwrites a1085				stringValue(3, "30"),1086				stringValue(4, "40"),1087			},1088			exp: []tsm1.StringValue{1089				stringValue(1, "10"),1090				stringValue(2, "20"),1091				stringValue(3, "30"),1092				stringValue(4, "40"),1093			},1094		},1095		{1096			a: []tsm1.StringValue{1097				stringValue(0, "0"),1098				stringValue(1, "1"),1099				stringValue(2, "2"),1100				stringValue(3, "3"),1101				stringValue(4, "4"),1102			},1103			b: []tsm1.StringValue{1104				stringValue(0, "0"),1105				stringValue(2, "20"),1106				stringValue(4, "40"),1107			},1108			exp: []tsm1.StringValue{1109				stringValue(0, "0.0"),1110				stringValue(1, "1"),1111				stringValue(2, "20"),1112				stringValue(3, "3"),1113				stringValue(4, "40"),1114			},1115		},1116	}1117	for i, test := range tests {1118		if i != 2 {1119			continue1120		}1121		got := tsm1.StringValues(test.a).Merge(test.b)1122		if exp, got := len(test.exp), len(got); exp != got {1123			t.Fatalf("test(%d): value length mismatch: exp %v, got %v", i, exp, got)1124		}1125		dedup := tsm1.StringValues(append(test.a, test.b...)).Deduplicate()1126		for i := range test.exp {1127			if exp, got := test.exp[i].String(), got[i].String(); exp != got {1128				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)1129			}1130			if exp, got := test.exp[i].String(), dedup[i].String(); exp != got {1131				t.Fatalf("value mismatch:\n exp %v\n got %v", exp, got)1132			}1133		}1134	}1135}1136func getTimes(n, step int, precision time.Duration) []int64 {1137	t := time.Now().Round(precision).UnixNano()1138	a := make([]int64, n)1139	for i := 0; i < n; i++ {1140		a[i] = t + (time.Duration(i*60) * precision).Nanoseconds()1141	}1142	return a1143}1144func BenchmarkDecodeBlock_Float_Empty(b *testing.B) {1145	valueCount := 10001146	times := getTimes(valueCount, 60, time.Second)1147	values := make([]tsm1.Value, len(times))1148	for i, t := range times {1149		values[i] = tsm1.NewValue(t, float64(i))1150	}1151	bytes, err := tsm1.Values(values).Encode(nil)1152	if err != nil {1153		b.Fatalf("unexpected error: %v", err)1154	}1155	var decodedValues []tsm1.Value1156	b.ResetTimer()1157	for i := 0; i < b.N; i++ {1158		_, err = tsm1.DecodeBlock(bytes, decodedValues)1159		if err != nil {1160			b.Fatalf("unexpected error decoding block: %v", err)1161		}1162	}1163}1164func BenchmarkDecodeBlock_Float_EqualSize(b *testing.B) {1165	valueCount := 10001166	times := getTimes(valueCount, 60, time.Second)1167	values := make([]tsm1.Value, len(times))1168	for i, t := range times {1169		values[i] = tsm1.NewValue(t, float64(i))1170	}1171	bytes, err := tsm1.Values(values).Encode(nil)1172	if err != nil {1173		b.Fatalf("unexpected error: %v", err)1174	}1175	decodedValues := make([]tsm1.Value, len(values))1176	b.ResetTimer()1177	for i := 0; i < b.N; i++ {1178		_, err = tsm1.DecodeBlock(bytes, decodedValues)1179		if err != nil {1180			b.Fatalf("unexpected error decoding block: %v", err)1181		}1182	}1183}1184func BenchmarkDecodeBlock_Float_TypeSpecific(b *testing.B) {1185	valueCount := 10001186	times := getTimes(valueCount, 60, time.Second)1187	values := make([]tsm1.Value, len(times))1188	for i, t := range times {1189		values[i] = tsm1.NewValue(t, float64(i))1190	}1191	bytes, err := tsm1.Values(values).Encode(nil)1192	if err != nil {1193		b.Fatalf("unexpected error: %v", err)1194	}1195	decodedValues := make([]tsm1.FloatValue, len(values))1196	b.ResetTimer()1197	for i := 0; i < b.N; i++ {1198		_, err = tsm1.DecodeFloatBlock(bytes, &decodedValues)1199		if err != nil {1200			b.Fatalf("unexpected error decoding block: %v", err)1201		}1202	}1203}1204func BenchmarkDecodeBlock_Integer_Empty(b *testing.B) {1205	valueCount := 10001206	times := getTimes(valueCount, 60, time.Second)1207	values := make([]tsm1.Value, len(times))1208	for i, t := range times {1209		values[i] = tsm1.NewValue(t, int64(i))1210	}1211	bytes, err := tsm1.Values(values).Encode(nil)1212	if err != nil {1213		b.Fatalf("unexpected error: %v", err)1214	}1215	var decodedValues []tsm1.Value1216	b.ResetTimer()1217	for i := 0; i < b.N; i++ {1218		_, err = tsm1.DecodeBlock(bytes, decodedValues)1219		if err != nil {1220			b.Fatalf("unexpected error decoding block: %v", err)1221		}1222	}1223}1224func BenchmarkDecodeBlock_Integer_EqualSize(b *testing.B) {1225	valueCount := 10001226	times := getTimes(valueCount, 60, time.Second)1227	values := make([]tsm1.Value, len(times))1228	for i, t := range times {1229		values[i] = tsm1.NewValue(t, int64(i))1230	}1231	bytes, err := tsm1.Values(values).Encode(nil)1232	if err != nil {1233		b.Fatalf("unexpected error: %v", err)1234	}1235	decodedValues := make([]tsm1.Value, len(values))1236	b.ResetTimer()1237	for i := 0; i < b.N; i++ {1238		_, err = tsm1.DecodeBlock(bytes, decodedValues)1239		if err != nil {1240			b.Fatalf("unexpected error decoding block: %v", err)1241		}1242	}1243}1244func BenchmarkDecodeBlock_Integer_TypeSpecific(b *testing.B) {1245	valueCount := 10001246	times := getTimes(valueCount, 60, time.Second)1247	values := make([]tsm1.Value, len(times))1248	for i, t := range times {1249		values[i] = tsm1.NewValue(t, int64(i))1250	}1251	bytes, err := tsm1.Values(values).Encode(nil)1252	if err != nil {1253		b.Fatalf("unexpected error: %v", err)1254	}1255	decodedValues := make([]tsm1.IntegerValue, len(values))1256	b.ResetTimer()1257	for i := 0; i < b.N; i++ {1258		_, err = tsm1.DecodeIntegerBlock(bytes, &decodedValues)1259		if err != nil {1260			b.Fatalf("unexpected error decoding block: %v", err)1261		}1262	}1263}1264func BenchmarkDecodeBlock_Boolean_Empty(b *testing.B) {1265	valueCount := 10001266	times := getTimes(valueCount, 60, time.Second)1267	values := make([]tsm1.Value, len(times))1268	for i, t := range times {1269		values[i] = tsm1.NewValue(t, true)1270	}1271	bytes, err := tsm1.Values(values).Encode(nil)1272	if err != nil {1273		b.Fatalf("unexpected error: %v", err)1274	}1275	var decodedValues []tsm1.Value1276	b.ResetTimer()1277	for i := 0; i < b.N; i++ {1278		_, err = tsm1.DecodeBlock(bytes, decodedValues)1279		if err != nil {1280			b.Fatalf("unexpected error decoding block: %v", err)1281		}1282	}1283}1284func BenchmarkDecodeBlock_Boolean_EqualSize(b *testing.B) {1285	valueCount := 10001286	times := getTimes(valueCount, 60, time.Second)1287	values := make([]tsm1.Value, len(times))1288	for i, t := range times {1289		values[i] = tsm1.NewValue(t, true)1290	}1291	bytes, err := tsm1.Values(values).Encode(nil)1292	if err != nil {1293		b.Fatalf("unexpected error: %v", err)1294	}1295	decodedValues := make([]tsm1.Value, len(values))1296	b.ResetTimer()1297	for i := 0; i < b.N; i++ {1298		_, err = tsm1.DecodeBlock(bytes, decodedValues)1299		if err != nil {1300			b.Fatalf("unexpected error decoding block: %v", err)1301		}1302	}1303}1304func BenchmarkDecodeBlock_Boolean_TypeSpecific(b *testing.B) {1305	valueCount := 10001306	times := getTimes(valueCount, 60, time.Second)1307	values := make([]tsm1.Value, len(times))1308	for i, t := range times {1309		values[i] = tsm1.NewValue(t, true)1310	}1311	bytes, err := tsm1.Values(values).Encode(nil)1312	if err != nil {1313		b.Fatalf("unexpected error: %v", err)1314	}1315	decodedValues := make([]tsm1.BooleanValue, len(values))1316	b.ResetTimer()1317	for i := 0; i < b.N; i++ {1318		_, err = tsm1.DecodeBooleanBlock(bytes, &decodedValues)1319		if err != nil {1320			b.Fatalf("unexpected error decoding block: %v", err)1321		}1322	}1323}1324func BenchmarkDecodeBooleanBlock(b *testing.B) {1325	cases := []int{1326		5,1327		55,1328		555,1329		1000,1330	}1331	for _, n := range cases {1332		b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {1333			valueCount := n1334			times := getTimes(valueCount, 60, time.Second)1335			values := make([]tsm1.Value, len(times))1336			for i, t := range times {1337				values[i] = tsm1.NewValue(t, true)1338			}1339			bytes, err := tsm1.Values(values).Encode(nil)1340			if err != nil {1341				b.Fatalf("unexpected error: %v", err)1342			}1343			b.ResetTimer()1344			b.ReportAllocs()1345			b.SetBytes(int64(tsm1.Values(values).Size()))1346			b.RunParallel(func(pb *testing.PB) {1347				decodedValues := make([]tsm1.BooleanValue, len(values))1348				for pb.Next() {1349					_, err = tsm1.DecodeBooleanBlock(bytes, &decodedValues)1350					if err != nil {1351						b.Fatalf("unexpected error decoding block: %v", err)1352					}1353				}1354			})1355		})1356	}1357}1358func BenchmarkDecodeFloatBlock(b *testing.B) {1359	cases := []int{1360		5,1361		55,1362		555,1363		1000,1364	}1365	for _, n := range cases {1366		b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {1367			valueCount := n1368			times := getTimes(valueCount, 60, time.Second)1369			values := make([]tsm1.Value, len(times))1370			for i, t := range times {1371				values[i] = tsm1.NewValue(t, float64(i))1372			}1373			bytes, err := tsm1.Values(values).Encode(nil)1374			if err != nil {1375				b.Fatalf("unexpected error: %v", err)1376			}1377			b.ResetTimer()1378			b.ReportAllocs()1379			b.SetBytes(int64(tsm1.Values(values).Size()))1380			b.RunParallel(func(pb *testing.PB) {1381				decodedValues := make([]tsm1.FloatValue, len(values))1382				for pb.Next() {1383					_, err = tsm1.DecodeFloatBlock(bytes, &decodedValues)1384					if err != nil {1385						b.Fatalf("unexpected error decoding block: %v", err)1386					}1387				}1388			})1389		})1390	}1391}1392func BenchmarkDecodeIntegerBlock(b *testing.B) {1393	rle := func(i int) int64 { return int64(i) }1394	s8b := func(i int) int64 { return int64(i + int(rand.Int31n(10))) }1395	cases := []struct {1396		enc string1397		gen func(i int) int641398		n   int1399	}{1400		{enc: "rle", gen: rle, n: 5},1401		{enc: "rle", gen: rle, n: 55},1402		{enc: "rle", gen: rle, n: 555},1403		{enc: "rle", gen: rle, n: 1000},1404		{enc: "s8b", gen: s8b, n: 5},1405		{enc: "s8b", gen: s8b, n: 55},1406		{enc: "s8b", gen: s8b, n: 555},1407		{enc: "s8b", gen: s8b, n: 1000},1408	}1409	for _, bm := range cases {1410		b.Run(fmt.Sprintf("%s_%d", bm.enc, bm.n), func(b *testing.B) {1411			rand.Seed(int64(bm.n * 1e3))1412			valueCount := bm.n1413			times := getTimes(valueCount, 60, time.Second)1414			values := make([]tsm1.Value, len(times))1415			for i, t := range times {1416				values[i] = tsm1.NewValue(t, bm.gen(i))1417			}1418			bytes, err := tsm1.Values(values).Encode(nil)1419			if err != nil {1420				b.Fatalf("unexpected error: %v", err)1421			}1422			b.ResetTimer()1423			b.ReportAllocs()1424			b.SetBytes(int64(tsm1.Values(values).Size()))1425			b.RunParallel(func(pb *testing.PB) {1426				decodedValues := make([]tsm1.IntegerValue, len(values))1427				for pb.Next() {1428					_, err = tsm1.DecodeIntegerBlock(bytes, &decodedValues)1429					if err != nil {1430						b.Fatalf("unexpected error decoding block: %v", err)1431					}1432				}1433			})1434		})1435	}1436}1437func BenchmarkDecodeStringBlock(b *testing.B) {1438	cases := []int{1439		5,1440		55,1441		555,1442		1000,1443	}1444	for _, n := range cases {1445		b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {1446			valueCount := n1447			times := getTimes(valueCount, 60, time.Second)1448			values := make([]tsm1.Value, len(times))1449			for i, t := range times {1450				values[i] = tsm1.NewValue(t, fmt.Sprintf("value %d", i))1451			}1452			bytes, err := tsm1.Values(values).Encode(nil)1453			if err != nil {1454				b.Fatalf("unexpected error: %v", err)1455			}1456			b.ResetTimer()1457			b.ReportAllocs()1458			b.SetBytes(int64(tsm1.Values(values).Size()))1459			b.RunParallel(func(pb *testing.PB) {1460				decodedValues := make([]tsm1.StringValue, len(values))1461				for pb.Next() {1462					_, err = tsm1.DecodeStringBlock(bytes, &decodedValues)1463					if err != nil {1464						b.Fatalf("unexpected error decoding block: %v", err)1465					}1466				}1467			})1468		})1469	}1470}1471func BenchmarkDecodeBlock_String_Empty(b *testing.B) {1472	valueCount := 10001473	times := getTimes(valueCount, 60, time.Second)1474	values := make([]tsm1.Value, len(times))1475	for i, t := range times {1476		values[i] = tsm1.NewValue(t, fmt.Sprintf("value %d", i))1477	}1478	bytes, err := tsm1.Values(values).Encode(nil)1479	if err != nil {1480		b.Fatalf("unexpected error: %v", err)1481	}1482	var decodedValues []tsm1.Value1483	b.ResetTimer()1484	for i := 0; i < b.N; i++ {1485		_, err = tsm1.DecodeBlock(bytes, decodedValues)1486		if err != nil {1487			b.Fatalf("unexpected error decoding block: %v", err)1488		}1489	}1490}1491func BenchmarkDecodeBlock_String_EqualSize(b *testing.B) {1492	valueCount := 10001493	times := getTimes(valueCount, 60, time.Second)1494	values := make([]tsm1.Value, len(times))1495	for i, t := range times {1496		values[i] = tsm1.NewValue(t, fmt.Sprintf("value %d", i))1497	}1498	bytes, err := tsm1.Values(values).Encode(nil)1499	if err != nil {1500		b.Fatalf("unexpected error: %v", err)1501	}1502	decodedValues := make([]tsm1.Value, len(values))1503	b.ResetTimer()1504	for i := 0; i < b.N; i++ {1505		_, err = tsm1.DecodeBlock(bytes, decodedValues)1506		if err != nil {1507			b.Fatalf("unexpected error decoding block: %v", err)1508		}1509	}1510}1511func BenchmarkDecodeBlock_String_TypeSpecific(b *testing.B) {1512	valueCount := 10001513	times := getTimes(valueCount, 60, time.Second)1514	values := make([]tsm1.Value, len(times))1515	for i, t := range times {1516		values[i] = tsm1.NewValue(t, fmt.Sprintf("value %d", i))1517	}1518	bytes, err := tsm1.Values(values).Encode(nil)1519	if err != nil {1520		b.Fatalf("unexpected error: %v", err)1521	}1522	decodedValues := make([]tsm1.StringValue, len(values))1523	b.ResetTimer()1524	for i := 0; i < b.N; i++ {1525		_, err = tsm1.DecodeStringBlock(bytes, &decodedValues)1526		if err != nil {1527			b.Fatalf("unexpected error decoding block: %v", err)1528		}1529	}1530}1531func BenchmarkValues_Deduplicate(b *testing.B) {1532	valueCount := 10001533	times := getTimes(valueCount, 60, time.Second)1534	values := make([]tsm1.Value, len(times))1535	for i, t := range times {1536		values[i] = tsm1.NewValue(t, fmt.Sprintf("value %d", i))1537	}1538	values = append(values, values...)1539	b.ResetTimer()1540	for i := 0; i < b.N; i++ {1541		tsm1.Values(values).Deduplicate()1542	}1543}1544func BenchmarkValues_Merge(b *testing.B) {1545	valueCount := 10001546	times := getTimes(valueCount, 60, time.Second)1547	a := make([]tsm1.Value, len(times))1548	c := make([]tsm1.Value, len(times))1549	for i, t := range times {1550		a[i] = tsm1.NewValue(t, float64(i))1551		c[i] = tsm1.NewValue(t+1, float64(i))1552	}1553	b.ResetTimer()1554	benchmarkMerge(a, c, b)1555}1556func BenchmarkValues_MergeDisjoint(b *testing.B) {1557	valueCount := 10001558	times := getTimes(valueCount, 60, time.Second)1559	a := make([]tsm1.Value, len(times))1560	c := make([]tsm1.Value, len(times))1561	for i, t := range times {1562		a[i] = tsm1.NewValue(t, float64(i))1563		c[i] = tsm1.NewValue(times[len(times)-1]+int64((i+1)*1e9), float64(i))1564	}1565	b.ResetTimer()1566	benchmarkMerge(a, c, b)1567}1568func BenchmarkValues_MergeSame(b *testing.B) {1569	valueCount := 10001570	times := getTimes(valueCount, 60, time.Second)1571	a := make([]tsm1.Value, len(times))1572	c := make([]tsm1.Value, len(times))1573	for i, t := range times {1574		a[i] = tsm1.NewValue(t, float64(i))1575		c[i] = tsm1.NewValue(t, float64(i))1576	}1577	b.ResetTimer()1578	benchmarkMerge(a, c, b)1579}1580func BenchmarkValues_MergeSimilar(b *testing.B) {1581	valueCount := 10001582	times := getTimes(valueCount, 60, time.Second)1583	a := make([]tsm1.Value, len(times))1584	c := make([]tsm1.Value, len(times))1585	for i, t := range times {1586		a[i] = tsm1.NewValue(t, float64(i))1587		if i == 0 {1588			t++1589		}1590		c[i] = tsm1.NewValue(t, float64(i))1591	}1592	b.ResetTimer()1593	benchmarkMerge(a, c, b)1594}1595func BenchmarkValues_MergeUnevenA(b *testing.B) {1596	valueCount := 10001597	times := getTimes(valueCount, 60, time.Second)1598	a := make([]tsm1.Value, len(times))1599	c := make([]tsm1.Value, len(times))1600	for i, t := range times {1601		a[i] = tsm1.NewValue(t, float64(i))1602		c[i] = tsm1.NewValue(t, float64(i))1603	}1604	b.ResetTimer()1605	benchmarkMerge(a[:700], c[:10], b)1606}1607func BenchmarkValues_MergeUnevenB(b *testing.B) {1608	valueCount := 10001609	times := getTimes(valueCount, 60, time.Second)1610	a := make([]tsm1.Value, len(times))1611	c := make([]tsm1.Value, len(times))1612	for i, t := range times {1613		a[i] = tsm1.NewValue(t, float64(i))1614		c[i] = tsm1.NewValue(t, float64(i))1615	}1616	b.ResetTimer()1617	benchmarkMerge(a[:10], c[:700], b)1618}1619func benchmarkMerge(a, c tsm1.Values, b *testing.B) {1620	for i := 0; i < b.N; i++ {1621		b.StopTimer()1622		aa := make(tsm1.Values, len(a))1623		copy(aa, a)1624		cc := make(tsm1.Values, len(c))1625		copy(cc, c)1626		b.StartTimer()1627		tsm1.Values(aa).Merge(tsm1.Values(cc))1628	}1629}1630func BenchmarkValues_EncodeInteger(b *testing.B) {1631	valueCount := 10241632	times := getTimes(valueCount, 60, time.Second)1633	a := make([]tsm1.Value, len(times))1634	for i, t := range times {1635		a[i] = tsm1.NewValue(t, int64(i))1636	}1637	buf := make([]byte, 1024*8)1638	b.ResetTimer()1639	for i := 0; i < b.N; i++ {1640		tsm1.Values(a).Encode(buf)1641	}1642}1643func BenchmarkValues_EncodeFloat(b *testing.B) {1644	valueCount := 10241645	times := getTimes(valueCount, 60, time.Second)1646	a := make([]tsm1.Value, len(times))1647	for i, t := range times {1648		a[i] = tsm1.NewValue(t, float64(i))1649	}1650	buf := make([]byte, 1024*8)1651	b.ResetTimer()1652	for i := 0; i < b.N; i++ {1653		tsm1.Values(a).Encode(buf)1654	}1655}1656func BenchmarkValues_EncodeString(b *testing.B) {1657	valueCount := 10241658	times := getTimes(valueCount, 60, time.Second)1659	a := make([]tsm1.Value, len(times))1660	for i, t := range times {1661		a[i] = tsm1.NewValue(t, fmt.Sprintf("%d", i))1662	}1663	buf := make([]byte, 1024*8)1664	b.ResetTimer()1665	for i := 0; i < b.N; i++ {1666		tsm1.Values(a).Encode(buf)1667	}1668}1669func BenchmarkValues_EncodeBool(b *testing.B) {1670	valueCount := 10241671	times := getTimes(valueCount, 60, time.Second)1672	a := make([]tsm1.Value, len(times))1673	for i, t := range times {1674		if i%2 == 0 {1675			a[i] = tsm1.NewValue(t, true)1676		} else {1677			a[i] = tsm1.NewValue(t, false)1678		}1679	}1680	buf := make([]byte, 1024*8)1681	b.ResetTimer()1682	for i := 0; i < b.N; i++ {1683		tsm1.Values(a).Encode(buf)1684	}1685}...

Full Screen

Full Screen

cron_test.go

Source:cron_test.go Github

copy

Full Screen

...99func TestCron_ErrorCronParse(t *testing.T) {100	controller := gomock.NewController(t)101	defer controller.Finish()102	mockTriggerer := mock.NewMockTriggerer(controller)103	mockTriggerer.EXPECT().Trigger(gomock.Any(), dummyRepo, gomock.Any()).Return(nil, nil).Times(1)104	mockRepos := mock.NewMockRepositoryStore(controller)105	mockRepos.EXPECT().Find(gomock.Any(), dummyCron.RepoID).Return(dummyRepo, nil).Times(1)106	mockCrons := mock.NewMockCronStore(controller)107	mockCrons.EXPECT().Ready(gomock.Any(), gomock.Any()).Return(dummyCronListInvalid, nil)108	mockCrons.EXPECT().Update(gomock.Any(), dummyCron).Times(1)109	mockUsers := mock.NewMockUserStore(controller)110	mockUsers.EXPECT().Find(gomock.Any(), dummyRepo.UserID).Return(dummyUser, nil).Times(1)111	mockCommits := mock.NewMockCommitService(controller)112	mockCommits.EXPECT().FindRef(gomock.Any(), dummyUser, dummyRepo.Slug, dummyRepo.Branch).Return(dummyCommit, nil).Times(1)113	s := Scheduler{114		commits: mockCommits,115		cron:    mockCrons,116		repos:   mockRepos,117		users:   mockUsers,118		trigger: mockTriggerer,119	}120	err := s.run(noContext)121	merr := err.(*multierror.Error)122	if got, want := len(merr.Errors), 1; got != want {123		t.Errorf("Want %d errors, got %d", want, got)124	}125}126// This unit tests demonstrates that if an error is encountered127// when finding the associated cron repository, the system will128// continue processing cron jobs and return an aggregated list of129// errors.130func TestCron_ErrorFindRepo(t *testing.T) {131	controller := gomock.NewController(t)132	defer controller.Finish()133	mockTriggerer := mock.NewMockTriggerer(controller)134	mockTriggerer.EXPECT().Trigger(gomock.Any(), dummyRepo, gomock.Any()).Return(nil, nil).Times(1)135	mockRepos := mock.NewMockRepositoryStore(controller)136	mockRepos.EXPECT().Find(gomock.Any(), dummyCron.RepoID).Return(dummyRepo, nil)137	mockRepos.EXPECT().Find(gomock.Any(), dummyCron.RepoID).Return(nil, sql.ErrNoRows)138	mockCrons := mock.NewMockCronStore(controller)139	mockCrons.EXPECT().Ready(gomock.Any(), gomock.Any()).Return(dummyCronListMultiple, nil)140	mockCrons.EXPECT().Update(gomock.Any(), dummyCron).Times(2)141	mockUsers := mock.NewMockUserStore(controller)142	mockUsers.EXPECT().Find(gomock.Any(), dummyRepo.UserID).Return(dummyUser, nil).Times(1)143	mockCommits := mock.NewMockCommitService(controller)144	mockCommits.EXPECT().FindRef(gomock.Any(), dummyUser, dummyRepo.Slug, dummyRepo.Branch).Return(dummyCommit, nil).Times(1)145	s := Scheduler{146		commits: mockCommits,147		cron:    mockCrons,148		repos:   mockRepos,149		users:   mockUsers,150		trigger: mockTriggerer,151	}152	err := s.run(noContext)153	merr := err.(*multierror.Error)154	if got, want := len(merr.Errors), 1; got != want {155		t.Errorf("Want %d errors, got %d", want, got)156	}157}158// This unit tests demonstrates that if an error is encountered159// when updating the next cron execution time, the system will160// continue processing cron jobs and return an aggregated list161// of errors.162func TestCron_ErrorUpdateCron(t *testing.T) {163	controller := gomock.NewController(t)164	defer controller.Finish()165	mockTriggerer := mock.NewMockTriggerer(controller)166	mockTriggerer.EXPECT().Trigger(gomock.Any(), dummyRepo, gomock.Any()).Return(nil, nil).Times(1)167	mockRepos := mock.NewMockRepositoryStore(controller)168	mockRepos.EXPECT().Find(gomock.Any(), dummyCron.RepoID).Return(dummyRepo, nil).Times(1)169	mockCrons := mock.NewMockCronStore(controller)170	mockCrons.EXPECT().Ready(gomock.Any(), gomock.Any()).Return(dummyCronListMultiple, nil)171	mockCrons.EXPECT().Update(gomock.Any(), dummyCron).Return(nil)172	mockCrons.EXPECT().Update(gomock.Any(), dummyCron).Return(sql.ErrNoRows)173	mockUsers := mock.NewMockUserStore(controller)174	mockUsers.EXPECT().Find(gomock.Any(), dummyRepo.UserID).Return(dummyUser, nil).Times(1)175	mockCommits := mock.NewMockCommitService(controller)176	mockCommits.EXPECT().FindRef(gomock.Any(), dummyUser, dummyRepo.Slug, dummyRepo.Branch).Return(dummyCommit, nil).Times(1)177	s := Scheduler{178		commits: mockCommits,179		cron:    mockCrons,180		repos:   mockRepos,181		users:   mockUsers,182		trigger: mockTriggerer,183	}184	err := s.run(noContext)185	merr := err.(*multierror.Error)186	if got, want := len(merr.Errors), 1; got != want {187		t.Errorf("Want %d errors, got %d", want, got)188	}189	if got, want := merr.Errors[0], sql.ErrNoRows; got != want {190		t.Errorf("Want error %v, got %v", want, got)191	}192}193// This unit tests demonstrates that if an error is encountered194// when finding the repository owner in the database, the system195// will continue processing cron jobs and return an aggregated196// list of errors.197func TestCron_ErrorFindUser(t *testing.T) {198	controller := gomock.NewController(t)199	defer controller.Finish()200	mockTriggerer := mock.NewMockTriggerer(controller)201	mockTriggerer.EXPECT().Trigger(gomock.Any(), dummyRepo, gomock.Any()).Return(nil, nil).Times(1)202	mockRepos := mock.NewMockRepositoryStore(controller)203	mockRepos.EXPECT().Find(gomock.Any(), dummyCron.RepoID).Return(dummyRepo, nil).Times(2)204	mockCrons := mock.NewMockCronStore(controller)205	mockCrons.EXPECT().Ready(gomock.Any(), gomock.Any()).Return(dummyCronListMultiple, nil)206	mockCrons.EXPECT().Update(gomock.Any(), dummyCron).Times(2)207	mockUsers := mock.NewMockUserStore(controller)208	mockUsers.EXPECT().Find(gomock.Any(), dummyRepo.UserID).Return(dummyUser, nil).Times(1)209	mockUsers.EXPECT().Find(gomock.Any(), dummyRepo.UserID).Return(nil, sql.ErrNoRows).Times(1)210	mockCommits := mock.NewMockCommitService(controller)211	mockCommits.EXPECT().FindRef(gomock.Any(), dummyUser, dummyRepo.Slug, dummyRepo.Branch).Return(dummyCommit, nil).Times(1)212	s := Scheduler{213		commits: mockCommits,214		cron:    mockCrons,215		repos:   mockRepos,216		users:   mockUsers,217		trigger: mockTriggerer,218	}219	err := s.run(noContext)220	merr := err.(*multierror.Error)221	if got, want := len(merr.Errors), 1; got != want {222		t.Errorf("Want %d errors, got %d", want, got)223	}224	if got, want := merr.Errors[0], sql.ErrNoRows; got != want {225		t.Errorf("Want error %v, got %v", want, got)226	}227}228// This unit tests demonstrates that if an error is encountered229// when communicating with the source code management system, the230// system will continue processing cron jobs and return an aggregated231// list of errors.232func TestCron_ErrorFindCommit(t *testing.T) {233	controller := gomock.NewController(t)234	defer controller.Finish()235	mockTriggerer := mock.NewMockTriggerer(controller)236	mockTriggerer.EXPECT().Trigger(gomock.Any(), dummyRepo, gomock.Any()).Return(nil, nil).Times(1)237	mockRepos := mock.NewMockRepositoryStore(controller)238	mockRepos.EXPECT().Find(gomock.Any(), dummyCron.RepoID).Return(dummyRepo, nil).Times(2)239	mockCrons := mock.NewMockCronStore(controller)240	mockCrons.EXPECT().Ready(gomock.Any(), gomock.Any()).Return(dummyCronListMultiple, nil)241	mockCrons.EXPECT().Update(gomock.Any(), dummyCron).Times(2)242	mockUsers := mock.NewMockUserStore(controller)243	mockUsers.EXPECT().Find(gomock.Any(), dummyRepo.UserID).Return(dummyUser, nil).Times(2)244	mockCommits := mock.NewMockCommitService(controller)245	mockCommits.EXPECT().FindRef(gomock.Any(), dummyUser, dummyRepo.Slug, dummyRepo.Branch).Return(dummyCommit, nil).Times(1)246	mockCommits.EXPECT().FindRef(gomock.Any(), dummyUser, dummyRepo.Slug, dummyRepo.Branch).Return(nil, sql.ErrNoRows).Times(1)247	s := Scheduler{248		commits: mockCommits,249		cron:    mockCrons,250		repos:   mockRepos,251		users:   mockUsers,252		trigger: mockTriggerer,253	}254	err := s.run(noContext)255	merr := err.(*multierror.Error)256	if got, want := len(merr.Errors), 1; got != want {257		t.Errorf("Want %d errors, got %d", want, got)258	}259	if got, want := merr.Errors[0], sql.ErrNoRows; got != want {260		t.Errorf("Want error %v, got %v", want, got)261	}262}263// This unit tests demonstrates that if an error is encountered264// when triggering a build, the system will continue processing265// cron jobs and return an aggregated list of errors.266func TestCron_ErrorTrigger(t *testing.T) {267	controller := gomock.NewController(t)268	defer controller.Finish()269	mockTriggerer := mock.NewMockTriggerer(controller)270	mockTriggerer.EXPECT().Trigger(gomock.Any(), dummyRepo, gomock.Any()).Return(nil, sql.ErrNoRows)271	mockTriggerer.EXPECT().Trigger(gomock.Any(), dummyRepo, gomock.Any()).Return(nil, nil)272	mockRepos := mock.NewMockRepositoryStore(controller)273	mockRepos.EXPECT().Find(gomock.Any(), dummyCron.RepoID).Return(dummyRepo, nil).Times(2)274	mockCrons := mock.NewMockCronStore(controller)275	mockCrons.EXPECT().Ready(gomock.Any(), gomock.Any()).Return(dummyCronListMultiple, nil)276	mockCrons.EXPECT().Update(gomock.Any(), dummyCron).Times(2)277	mockUsers := mock.NewMockUserStore(controller)278	mockUsers.EXPECT().Find(gomock.Any(), dummyRepo.UserID).Return(dummyUser, nil).Times(2)279	mockCommits := mock.NewMockCommitService(controller)280	mockCommits.EXPECT().FindRef(gomock.Any(), dummyUser, dummyRepo.Slug, dummyRepo.Branch).Return(dummyCommit, nil).Times(2)281	s := Scheduler{282		commits: mockCommits,283		cron:    mockCrons,284		repos:   mockRepos,285		users:   mockUsers,286		trigger: mockTriggerer,287	}288	err := s.run(noContext)289	merr := err.(*multierror.Error)290	if got, want := len(merr.Errors), 1; got != want {291		t.Errorf("Want %d errors, got %d", want, got)292	}293	if got, want := merr.Errors[0], sql.ErrNoRows; got != want {294		t.Errorf("Want error %v, got %v", want, got)295	}296}297var (298	noContext = context.Background()299	dummyUser = &core.User{300		Login: "octocat",301	}302	dummyBuild = &core.Build{303		Number:       dummyRepo.Counter,304		RepoID:       dummyRepo.ID,305		Status:       core.StatusPending,306		Event:        core.EventCron,307		Link:         "https://github.com/octocat/Hello-World/commit/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",308		Timestamp:    1299283200,309		Message:      "first commit",310		Before:       "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",311		After:        "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",312		Ref:          "refs/heads/master",313		Source:       "master",314		Target:       "master",315		Author:       "octocat",316		AuthorName:   "The Octocat",317		AuthorEmail:  "octocat@hello-world.com",318		AuthorAvatar: "https://avatars3.githubusercontent.com/u/583231",319		Sender:       "octocat",320	}321	dummyRepo = &core.Repository{322		ID:         1,323		UID:        "1296269",324		UserID:     2,325		Namespace:  "octocat",326		Name:       "Hello-World",327		Slug:       "octocat/Hello-World",328		SCM:        "git",329		HTTPURL:    "https://github.com/octocat/Hello-World.git",330		SSHURL:     "git@github.com:octocat/Hello-World.git",331		Link:       "https://github.com/octocat/Hello-World",332		Branch:     "master",333		Private:    false,334		Visibility: core.VisibilityPublic,335		Active:     true,336		Counter:    42,337		Signer:     "g9dMChy22QutQM5lrpbe0yCR3f15t1gv",338		Secret:     "g9dMChy22QutQM5lrpbe0yCR3f15t1gv",339	}340	dummyCron = &core.Cron{341		RepoID: dummyRepo.ID,342		Name:   "nightly",343		Expr:   "0 0 * * *",344		Next:   2000000000,345		Prev:   1000000000,346		Branch: "master",347	}348	dummyCronInvalid = &core.Cron{349		RepoID: dummyRepo.ID,350		Name:   "nightly",351		Expr:   "A B C D E",352		Next:   2000000000,353		Prev:   1000000000,354		Branch: "master",355	}356	dummyCronList = []*core.Cron{357		dummyCron,358	}359	dummyCronListMultiple = []*core.Cron{360		dummyCron,361		dummyCron,362	}363	dummyCronListInvalid = []*core.Cron{364		dummyCronInvalid,365		dummyCron,366	}367	dummyHook = &core.Hook{368		Event:        core.EventCron,369		Link:         "https://github.com/octocat/Hello-World/commit/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",370		Timestamp:    1299283200,371		Message:      "first commit",372		Before:       "553c2077f0edc3d5dc5d17262f6aa498e69d6f8e",373		After:        "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",374		Ref:          "refs/heads/master",375		Source:       "master",376		Target:       "master",377		Author:       "octocat",378		AuthorName:   "The Octocat",379		AuthorEmail:  "octocat@hello-world.com",380		AuthorAvatar: "https://avatars3.githubusercontent.com/u/583231",381		Sender:       "octocat",382		Cron:         "nightly",383		Trigger:      "@cron",384	}385	dummyCommit = &core.Commit{386		Sha:     dummyHook.After,387		Message: dummyHook.Message,388		Link:    dummyHook.Link,389		Committer: &core.Committer{390			Name:   dummyHook.AuthorName,391			Email:  dummyHook.AuthorEmail,392			Login:  dummyHook.Author,393			Avatar: dummyHook.AuthorAvatar,394			Date:   dummyHook.Timestamp,395		},396		Author: &core.Committer{397			Name:   dummyHook.AuthorName,398			Email:  dummyHook.AuthorEmail,399			Login:  dummyHook.Author,400			Avatar: dummyHook.AuthorAvatar,401			Date:   dummyHook.Timestamp,402		},403	}404	ignoreBuildFields = cmpopts.IgnoreFields(core.Build{},405		"Created", "Updated")406)...

Full Screen

Full Screen

bundler_test.go

Source:bundler_test.go Github

copy

Full Screen

...36		t.Errorf("bundles: got %v, want %v", got, want)37	}38	// All bundles should have been handled "immediately": much less39	// than the delay threshold of 1s.40	tgot := quantizeTimes(handler.times(), 100*time.Millisecond)41	twant := []int{0, 0, 0}42	if !reflect.DeepEqual(tgot, twant) {43		t.Errorf("times: got %v, want %v", tgot, twant)44	}45}46func TestBundlerCount3(t *testing.T) {47	handler := &testHandler{}48	b := NewBundler(int(0), handler.handle)49	b.BundleCountThreshold = 350	b.DelayThreshold = 100 * time.Millisecond51	// Add 8 items.52	// The first two bundles of 3 should both be handled quickly.53	// The third bundle of 2 should not be handled for about DelayThreshold ms.54	for i := 0; i < 8; i++ {55		if err := b.Add(i, 1); err != nil {56			t.Fatal(err)57		}58	}59	time.Sleep(5 * b.DelayThreshold)60	// We should not need to close the bundler.61	bgot := handler.bundles()62	bwant := [][]int{{0, 1, 2}, {3, 4, 5}, {6, 7}}63	if !reflect.DeepEqual(bgot, bwant) {64		t.Errorf("bundles: got %v, want %v", bgot, bwant)65	}66	tgot := quantizeTimes(handler.times(), b.DelayThreshold)67	if len(tgot) != 3 || tgot[0] != 0 || tgot[1] != 0 || tgot[2] == 0 {68		t.Errorf("times: got %v, want [0, 0, non-zero]", tgot)69	}70}71func TestBundlerByteThreshold(t *testing.T) {72	handler := &testHandler{}73	b := NewBundler(int(0), handler.handle)74	b.BundleCountThreshold = 1075	b.BundleByteThreshold = 376	add := func(i interface{}, s int) {77		if err := b.Add(i, s); err != nil {78			t.Fatal(err)79		}80	}81	add(1, 1)82	add(2, 2)83	// Hit byte threshold: bundle = 1, 284	add(3, 1)85	add(4, 1)86	add(5, 2)87	// Passed byte threshold, but not limit: bundle = 3, 4, 588	add(6, 1)89	b.Close()90	bgot := handler.bundles()91	bwant := [][]int{{1, 2}, {3, 4, 5}, {6}}92	if !reflect.DeepEqual(bgot, bwant) {93		t.Errorf("bundles: got %v, want %v", bgot, bwant)94	}95	tgot := quantizeTimes(handler.times(), b.DelayThreshold)96	twant := []int{0, 0, 0}97	if !reflect.DeepEqual(tgot, twant) {98		t.Errorf("times: got %v, want %v", tgot, twant)99	}100}101func TestBundlerLimit(t *testing.T) {102	handler := &testHandler{}103	b := NewBundler(int(0), handler.handle)104	b.BundleCountThreshold = 10105	b.BundleByteLimit = 3106	add := func(i interface{}, s int) {107		if err := b.Add(i, s); err != nil {108			t.Fatal(err)109		}110	}111	add(1, 1)112	add(2, 2)113	// Hit byte limit: bundle = 1, 2114	add(3, 1)115	add(4, 1)116	add(5, 2)117	// Exceeded byte limit: bundle = 3, 4118	add(6, 2)119	// Exceeded byte limit: bundle = 5120	b.Close()121	bgot := handler.bundles()122	bwant := [][]int{{1, 2}, {3, 4}, {5}, {6}}123	if !reflect.DeepEqual(bgot, bwant) {124		t.Errorf("bundles: got %v, want %v", bgot, bwant)125	}126	tgot := quantizeTimes(handler.times(), b.DelayThreshold)127	twant := []int{0, 0, 0, 0}128	if !reflect.DeepEqual(tgot, twant) {129		t.Errorf("times: got %v, want %v", tgot, twant)130	}131}132func TestBundlerErrors(t *testing.T) {133	// Use a handler that blocks forever, to force the bundler to run out of134	// memory.135	b := NewBundler(int(0), func(interface{}) { select {} })136	b.BundleByteLimit = 3137	b.BufferedByteLimit = 10138	if got, want := b.Add(1, 4), ErrOversizedItem; got != want {139		t.Fatalf("got %v, want %v", got, want)140	}141	for i := 0; i < 5; i++ {142		if err := b.Add(i, 2); err != nil {143			t.Fatal(err)144		}145	}146	if got, want := b.Add(5, 1), ErrOverflow; got != want {147		t.Fatalf("got %v, want %v", got, want)148	}149}150type testHandler struct {151	mu sync.Mutex152	b  [][]int153	t  []time.Time154}155func (t *testHandler) bundles() [][]int {156	t.mu.Lock()157	defer t.mu.Unlock()158	return t.b159}160func (t *testHandler) times() []time.Time {161	t.mu.Lock()162	defer t.mu.Unlock()163	return t.t164}165func (t *testHandler) handle(b interface{}) {166	t.mu.Lock()167	defer t.mu.Unlock()168	t.b = append(t.b, b.([]int))169	t.t = append(t.t, time.Now())170}171// Round times to the nearest q and express them as the number of q172// since the first time.173// E.g. if q is 100ms, then a time within 50ms of the first time174// will be represented as 0, a time 150 to 250ms of the first time175// we be represented as 1, etc.176func quantizeTimes(times []time.Time, q time.Duration) []int {177	var rs []int178	for _, t := range times {179		d := t.Sub(times[0])180		r := int((d + q/2) / q)181		rs = append(rs, r)182	}183	return rs184}185func TestQuantizeTimes(t *testing.T) {186	quantum := 100 * time.Millisecond187	for _, test := range []struct {188		millis []int // times in milliseconds189		want   []int190	}{191		{[]int{10, 20, 30}, []int{0, 0, 0}},192		{[]int{0, 49, 50, 90}, []int{0, 0, 1, 1}},193		{[]int{0, 95, 170, 315}, []int{0, 1, 2, 3}},194	} {195		var times []time.Time196		for _, ms := range test.millis {197			times = append(times, time.Unix(0, int64(ms*1e6)))198		}199		got := quantizeTimes(times, quantum)200		if !reflect.DeepEqual(got, test.want) {201			t.Errorf("%v: got %v, want %v", test.millis, got, test.want)202		}203	}204}...

Full Screen

Full Screen

Times

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    t := time.Now()4    p(t.Format(time.RFC3339))5    t1, _ := time.Parse(6    p(t1)7    p(t.Format("3:04PM"))8    p(t.Format("Mon Jan _2 15:04:05 2006"))9    p(t.Format("2006-01-02T15:04:05.999999-07:00"))10    t2, _ := time.Parse(form, "8 41 PM")11    p(t2)12    fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:0013        t.Year(), t.Month(), t.Day(),14        t.Hour(), t.Minute(), t.Second())15    _, e := time.Parse(ansic, "8:41PM")16    p(e)17}18import (19func main() {20    t := time.Now()21    p(t)22    later := t.Add(23    p(later)24    diff := later.Sub(t)25    p(diff)26    p(diff.Hours())27    p(diff.Minutes())28    p(diff.Seconds())29    p(diff.N

Full Screen

Full Screen

Times

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "time"3func main() {4t := time.Now()5fmt.Println(t)6fmt.Println(t.Year())7fmt.Println(t.Month())8fmt.Println(t.Day())9fmt.Println(t.Hour())10fmt.Println(t.Minute())11fmt.Println(t.Second())12fmt.Println(t.Nanosecond())13fmt.Println(t.Location())14fmt.Println(t.Weekday())15fmt.Println(t.Zone())16fmt.Println(t.Unix())17fmt.Println(t.UnixNano())18}19import "fmt"20import "time"21func main() {22t := time.Now()23fmt.Println(t.Format(time.RFC3339))24fmt.Println(t.Format("3:04PM"))25fmt.Println(t.Format("Mon Jan _2 15:04:05 2006"))26fmt.Println(t.Format("2006-01-02T15:04:05.999999-07:00"))27fmt.Println(t.Format("Mon, 02 Jan 2006 15:04:05 MST"))28s := t.Format("20060102")29fmt.Println(t, "=>", s)30}31import "fmt"32import "time"33func main() {34t := time.Now()35p(t.Format(time.RFC3339))36t1, e := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")37p(t1)38fmt.Println(e)39fmt.Println(t.Format("3:04PM"))40fmt.Println(t.Format("Mon Jan _2 15:04:05 2006"))41fmt.Println(t.Format("2006-

Full Screen

Full Screen

Times

Using AI Code Generation

copy

Full Screen

1import (2func main() {3t := time.Now()4fmt.Println(t)5fmt.Println(t.Hour())6fmt.Println(t.Minute())7fmt.Println(t.Second())8fmt.Println(t.Nanosecond())9fmt.Println(t.Location())10fmt.Println(t.Weekday())11}12import (13func main() {14t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)15fmt.Println(t)16}17import (18func main() {19t, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")20if err != nil {21fmt.Println(err)22}23fmt.Println(t)24}25import (26func main() {27loc, _ := time.LoadLocation("America/Los_Angeles")28t, err := time.ParseInLocation(time.RFC3339, "2012-11-01T22:08:41+00:00", loc)29if err != nil {30fmt.Println(err)31}32fmt.Println(t)33}34import (35func main() {36t := time.Now()37fmt.Println(t.Format(time.RFC3339))38fmt.Println(t.Format(time.RFC822))39fmt.Println(t.Format(time.ANSIC))40fmt.Println(t.Format(time.UnixDate))41fmt.Println(t.Format(time.RubyDate))42fmt.Println(t.Format(time.RFC822Z))43fmt.Println(t.Format(time.RFC850))44fmt.Println(t.Format(time.Kitchen))45fmt.Println(t.Format(time.Stamp))46fmt.Println(t.Format(time.StampMilli))47fmt.Println(t.Format(time.StampMicro))48fmt.Println(t.Format(time.StampNano))49}50import (51func main() {52t := time.Now()53fmt.Println(t)54fmt.Println(t.Add(time.Hour))55fmt.Println(t.Add(-time

Full Screen

Full Screen

Times

Using AI Code Generation

copy

Full Screen

1func main() {2    t := time.Now()3    fmt.Println(t)4    fmt.Println(t.Format(time.RFC3339))5    fmt.Println(t.Year())6    fmt.Println(t.Month())7    fmt.Println(t.Day())8    fmt.Println(t.Hour())9    fmt.Println(t.Minute())10    fmt.Println(t.Second())11    fmt.Println(t.Nanosecond())12    fmt.Println(t.Location())13    fmt.Println(t.Weekday())14    fmt.Println(t.Before(time.Now()))15    fmt.Println(t.After(time.Now()))16    fmt.Println(t.Equal(time.Now()))17    fmt.Println(t.Add(time.Hour))18    fmt.Println(t.Add(-time.Hour))19    fmt.Println(t.Format("2006-01-02T15:04:05-07:00"))20    fmt.Println(t.Format("3:04PM"))21    fmt.Println(t.Format("Mon Jan _2 15:04:05 2006"))22    fmt.Println(t.Format("2006-01-02T15:04:05.999999-07:00"))23    fmt.Println(t.Format("2006-01-02T15:04:05Z07:00"))24    fmt.Println(t.Format(time.RFC3339))25    s := t.Format("20060102")26    fmt.Println(t, "=>", s)27    fmt.Println(t.Format("02/01/2006"))28    fmt.Println(t.Format("02/01/2006 15:04:05"))29    fmt.Println(t.Format("02/01/2006 15:04:05.000000"))30    fmt.Println(t.Format("02/01/2006 15:04:05.000000 MST"))31    fmt.Println(t.Format("02/01/2006 15:04:05.000000 -0700"))32    fmt.Println(t.Format("02/01/2006 15:04:05.000000 -07:00"))33    fmt.Println(t.Format("02/01/2006 15:04:05.000000 -07"))34    fmt.Println(t.Format("02/01/2006 15:04:05.000000 -07:00 MST"))35    fmt.Println(t.Format("02/01/2006 15:04:05.000000 -0700 MST"))36    fmt.Println(t.Format("02/01/2006 15:04:05.000000 -07 MST"))37    fmt.Println(t.Format("02/

Full Screen

Full Screen

Times

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	fmt.Println("Hello, playground")4	t := time.Now()5	fmt.Println(t)6	fmt.Println(t.Hour())7	fmt.Println(t.Minute())8	fmt.Println(t.Second())9	fmt.Println(t.Nanosecond())10	fmt.Println(t.Location())11	fmt.Println(t.Weekday())12	fmt.Println(t.Year())13	fmt.Println(t.Month())14	fmt.Println(t.Day())15	fmt.Println(t.Zone())16	fmt.Println(t.Unix())17	fmt.Println(t.UnixNano())18}19import (20func main() {21	fmt.Println("Hello, playground")22	t := time.Now()23	fmt.Println(t)24	fmt.Println(t.After(time.Now()))25	fmt.Println(t.After(t.Add(time.Second)))26}27import (28func main() {29	fmt.Println("Hello, playground")30	t := time.Now()31	fmt.Println(t)32	fmt.Println(t.Add(time.Second))33	fmt.Println(t.Add(time.Minute))34	fmt.Println(t.Add(time.Hour))35}36import (37func main()

Full Screen

Full Screen

Times

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	t := time.Now()4	fmt.Println("Time is: ", t)5	fmt.Println("Time is: ", t.Format("01-02-2006 15:04:05"))6	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05"))7	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday"))8	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January"))9	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday"))10	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006"))11	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006 04:05:15"))12	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006 04:05:15 PM"))13	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006 04:05:15 PM Monday"))14	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006 04:05:15 PM Monday Monday"))15	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006 04:05:15 PM Monday Monday Monday"))16	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006 04:05:15 PM Monday Monday Monday Monday"))17	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006 04:05:15 PM Monday Monday Monday Monday Monday"))18	fmt.Println("Time is: ", t.Format("02-01-2006 15:04:05 Monday January Monday 2006 04:05:15 PM Monday Monday Monday Monday Monday Monday"))19	fmt.Println("

Full Screen

Full Screen

Times

Using AI Code Generation

copy

Full Screen

1func main() {2    fmt.Println("Hello World")3    t1 := time.Now()4    fmt.Println("Time now: ", t1)5    fmt.Println("Time now: ", t1.Format("2006-01-02 15:04:05.999999999 -0700 MST"))6    fmt.Println("Time now: ", t1.Format(time.RFC3339))7    fmt.Println("Time now: ", t1.Format(time.RFC3339Nano))8    t2 := time.Now().Add(1000 * time.Millisecond)9    fmt.Println("Time now: ", t2)10    fmt.Println("Time now: ", t2.Format(time.RFC3339))11    fmt.Println("Time now: ", t2.Format(time.RFC3339Nano))12    fmt.Println("Time now: ", t2.Format("2006-01-02 15:04:05.999999999 -0700 MST"))13    fmt.Println("Time now: ", t2.Format("2006-01-02 15:04:05.999999999 -0700"))14    fmt.Println("Time now: ", t2.Format("2006-01-02 15:04:05.999999999"))15    fmt.Println("Time now: ", t2.Format("2006-01-02 15:04:05.999999"))16    fmt.Println("Time now: ", t2.Format("2006-01-02 15:04:05.999"))17    fmt.Println("Time now: ", t2.Format("2006-01-02 15:04:05"))18    fmt.Println("Time now: ", t2.Format("2006-01-02 15:04"))19    fmt.Println("Time now: ", t2.Format("2006-01-02 15"))20    fmt.Println("Time now: ", t2.Format("2006-01-02"))21    fmt.Println("Time now: ", t2.Format("2006-01"))22    fmt.Println("Time now: ", t2.Format("2006"))23    fmt.Println("Time now: ", t2.Format("06"))24    fmt.Println("Time now: ", t2.Format("2006-01-02 15:04:05.999999999 -0700 MST"))25}

Full Screen

Full Screen

Times

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	t := time.Now()4	fmt.Println(t.Hour(), t.Minute(), t.Second())5}6GoLang Time Minute() Method7func (t Time) Minute() int8import (9func main() {10	t := time.Now()11	fmt.Println(t.Minute())12}13GoLang Time Month() Method14func (t Time) Month() Month15import (16func main() {17	t := time.Now()18	fmt.Println(t.Month())19}20GoLang Time Nanosecond() Method21func (t Time) Nanosecond() int22import (23func main() {24	t := time.Now()25	fmt.Println(t.Nanosecond())26}27GoLang Time Second() Method28func (t Time) Second() int29import (30func main() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful