Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: common/tsmon/store/testing.go

Issue 1857643003: Revert of Migrate tsmon protos to proto3 (Closed) Base URL: git@github.com:luci/luci-go.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « common/tsmon/store/inmemory_test.go ('k') | common/tsmon/target/target.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package store 5 package store
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt" 9 "fmt"
10 "sort" 10 "sort"
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 test.wantSetValidator(v, test.va lues[0]) 255 test.wantSetValidator(v, test.va lues[0])
256 } else { 256 } else {
257 So(v, ShouldEqual, test.values[0 ]) 257 So(v, ShouldEqual, test.values[0 ])
258 } 258 }
259 259
260 // Check the time in the Cell is the sam e. 260 // Check the time in the Cell is the sam e.
261 all := s.GetAll(ctx) 261 all := s.GetAll(ctx)
262 So(len(all), ShouldEqual, 1) 262 So(len(all), ShouldEqual, 1)
263 263
264 msg := monitor.SerializeCell(all[0]) 264 msg := monitor.SerializeCell(all[0])
265 » » » » » So(time.Unix(0, int64(msg.StartTimestamp Us*uint64(time.Microsecond))).UTC().String(), 265 » » » » » So(time.Unix(0, int64(msg.GetStartTimest ampUs()*uint64(time.Microsecond))).UTC().String(),
266 ShouldEqual, t.String()) 266 ShouldEqual, t.String())
267 }) 267 })
268 } 268 }
269 }) 269 })
270 270
271 Convey("With a target set in the context", func() { 271 Convey("With a target set in the context", func() {
272 for i, test := range tests { 272 for i, test := range tests {
273 if !test.wantSetSuccess { 273 if !test.wantSetSuccess {
274 continue 274 continue
275 } 275 }
276 276
277 Convey(fmt.Sprintf("%d. %s", i, test.typ), func( ) { 277 Convey(fmt.Sprintf("%d. %s", i, test.typ), func( ) {
278 var m types.Metric 278 var m types.Metric
279 if test.bucketer != nil { 279 if test.bucketer != nil {
280 m = &fakeDistributionMetric{Fake Metric{"m", "", []field.Field{}, test.typ}, test.bucketer} 280 m = &fakeDistributionMetric{Fake Metric{"m", "", []field.Field{}, test.typ}, test.bucketer}
281 } else { 281 } else {
282 m = &FakeMetric{"m", "", []field .Field{}, test.typ} 282 m = &FakeMetric{"m", "", []field .Field{}, test.typ}
283 } 283 }
284 284
285 s := opts.Factory() 285 s := opts.Factory()
286 s.Register(m) 286 s.Register(m)
287 opts.RegistrationFinished(s) 287 opts.RegistrationFinished(s)
288 288
289 // Create a context with a different tar get. 289 // Create a context with a different tar get.
290 t := target.Task{} 290 t := target.Task{}
291 » » » » » t.AsProto().ServiceName = "foo" 291 » » » » » t.AsProto().ServiceName = proto.String(" foo")
292 ctxWithTarget := target.Set(ctx, &t) 292 ctxWithTarget := target.Set(ctx, &t)
293 293
294 // Set the first value on the default ta rget, second value on the 294 // Set the first value on the default ta rget, second value on the
295 // different target. 295 // different target.
296 So(s.Set(ctx, m, time.Time{}, []interfac e{}{}, test.values[0]), ShouldBeNil) 296 So(s.Set(ctx, m, time.Time{}, []interfac e{}{}, test.values[0]), ShouldBeNil)
297 So(s.Set(ctxWithTarget, m, time.Time{}, []interface{}{}, test.values[1]), ShouldBeNil) 297 So(s.Set(ctxWithTarget, m, time.Time{}, []interface{}{}, test.values[1]), ShouldBeNil)
298 298
299 // Get should return different values fo r different contexts. 299 // Get should return different values fo r different contexts.
300 v, err := s.Get(ctx, m, time.Time{}, []i nterface{}{}) 300 v, err := s.Get(ctx, m, time.Time{}, []i nterface{}{})
301 So(err, ShouldBeNil) 301 So(err, ShouldBeNil)
(...skipping 10 matching lines...) Expand all
312 } else { 312 } else {
313 So(v, ShouldEqual, test.values[1 ]) 313 So(v, ShouldEqual, test.values[1 ])
314 } 314 }
315 315
316 // The targets should be set in the Cell s. 316 // The targets should be set in the Cell s.
317 all := s.GetAll(ctx) 317 all := s.GetAll(ctx)
318 So(len(all), ShouldEqual, 2) 318 So(len(all), ShouldEqual, 2)
319 319
320 coll := monitor.SerializeCells(all) 320 coll := monitor.SerializeCells(all)
321 sort.Sort(sortableDataSlice(coll.Data)) 321 sort.Sort(sortableDataSlice(coll.Data))
322 » » » » » So(coll.Data[0].Task.ServiceName, Should Equal, 322 » » » » » So(coll.Data[0].Task.GetServiceName(), S houldEqual,
323 » » » » » » s.DefaultTarget().(*target.Task) .AsProto().ServiceName) 323 » » » » » » s.DefaultTarget().(*target.Task) .AsProto().GetServiceName())
324 » » » » » So(coll.Data[1].Task.ServiceName, Should Equal, t.AsProto().ServiceName) 324 » » » » » So(coll.Data[1].Task.GetServiceName(), S houldEqual, t.AsProto().GetServiceName())
325 }) 325 })
326 } 326 }
327 }) 327 })
328 328
329 Convey("With a decreasing value", func() { 329 Convey("With a decreasing value", func() {
330 for i, test := range tests { 330 for i, test := range tests {
331 if !test.typ.IsCumulative() || test.bucketer != nil { 331 if !test.typ.IsCumulative() || test.bucketer != nil {
332 continue 332 continue
333 } 333 }
334 Convey(fmt.Sprintf("%d. %s", i, test.typ), func( ) { 334 Convey(fmt.Sprintf("%d. %s", i, test.typ), func( ) {
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 459
460 // Do the incr with a fixed time. 460 // Do the incr with a fixed time.
461 t := time.Date(1972, 5, 6, 7, 8, 9, 0, t ime.UTC) 461 t := time.Date(1972, 5, 6, 7, 8, 9, 0, t ime.UTC)
462 So(s.Incr(ctx, m, t, []interface{}{}, te st.deltas[0]), ShouldBeNil) 462 So(s.Incr(ctx, m, t, []interface{}{}, te st.deltas[0]), ShouldBeNil)
463 463
464 // Check the time in the Cell is the sam e. 464 // Check the time in the Cell is the sam e.
465 all := s.GetAll(ctx) 465 all := s.GetAll(ctx)
466 So(len(all), ShouldEqual, 1) 466 So(len(all), ShouldEqual, 1)
467 467
468 msg := monitor.SerializeCell(all[0]) 468 msg := monitor.SerializeCell(all[0])
469 » » » » » So(time.Unix(0, int64(msg.StartTimestamp Us*uint64(time.Microsecond))).UTC().String(), 469 » » » » » So(time.Unix(0, int64(msg.GetStartTimest ampUs()*uint64(time.Microsecond))).UTC().String(),
470 ShouldEqual, t.String()) 470 ShouldEqual, t.String())
471 }) 471 })
472 } 472 }
473 }) 473 })
474 474
475 Convey("With a target set in the context", func() { 475 Convey("With a target set in the context", func() {
476 for i, test := range tests { 476 for i, test := range tests {
477 if !test.wantIncrSuccess { 477 if !test.wantIncrSuccess {
478 continue 478 continue
479 } 479 }
480 480
481 Convey(fmt.Sprintf("%d. %s", i, test.typ), func( ) { 481 Convey(fmt.Sprintf("%d. %s", i, test.typ), func( ) {
482 var m types.Metric 482 var m types.Metric
483 if test.bucketer != nil { 483 if test.bucketer != nil {
484 m = &fakeDistributionMetric{Fake Metric{"m", "", []field.Field{}, test.typ}, test.bucketer} 484 m = &fakeDistributionMetric{Fake Metric{"m", "", []field.Field{}, test.typ}, test.bucketer}
485 } else { 485 } else {
486 m = &FakeMetric{"m", "", []field .Field{}, test.typ} 486 m = &FakeMetric{"m", "", []field .Field{}, test.typ}
487 } 487 }
488 488
489 s := opts.Factory() 489 s := opts.Factory()
490 s.Register(m) 490 s.Register(m)
491 opts.RegistrationFinished(s) 491 opts.RegistrationFinished(s)
492 492
493 // Create a context with a different tar get. 493 // Create a context with a different tar get.
494 t := target.Task{} 494 t := target.Task{}
495 » » » » » t.AsProto().ServiceName = "foo" 495 » » » » » t.AsProto().ServiceName = proto.String(" foo")
496 ctxWithTarget := target.Set(ctx, &t) 496 ctxWithTarget := target.Set(ctx, &t)
497 497
498 // Incr the first delta on the default t arget, second delta on the 498 // Incr the first delta on the default t arget, second delta on the
499 // different target. 499 // different target.
500 So(s.Incr(ctx, m, time.Time{}, []interfa ce{}{}, test.deltas[0]), ShouldBeNil) 500 So(s.Incr(ctx, m, time.Time{}, []interfa ce{}{}, test.deltas[0]), ShouldBeNil)
501 So(s.Incr(ctxWithTarget, m, time.Time{}, []interface{}{}, test.deltas[1]), ShouldBeNil) 501 So(s.Incr(ctxWithTarget, m, time.Time{}, []interface{}{}, test.deltas[1]), ShouldBeNil)
502 502
503 // Get should return different values fo r different contexts. 503 // Get should return different values fo r different contexts.
504 v1, err := s.Get(ctx, m, time.Time{}, [] interface{}{}) 504 v1, err := s.Get(ctx, m, time.Time{}, [] interface{}{})
505 So(err, ShouldBeNil) 505 So(err, ShouldBeNil)
506 v2, err := s.Get(ctxWithTarget, m, time. Time{}, []interface{}{}) 506 v2, err := s.Get(ctxWithTarget, m, time. Time{}, []interface{}{})
507 So(err, ShouldBeNil) 507 So(err, ShouldBeNil)
508 So(v1, ShouldNotEqual, v2) 508 So(v1, ShouldNotEqual, v2)
509 509
510 // The targets should be set in the Cell s. 510 // The targets should be set in the Cell s.
511 all := s.GetAll(ctx) 511 all := s.GetAll(ctx)
512 So(len(all), ShouldEqual, 2) 512 So(len(all), ShouldEqual, 2)
513 513
514 coll := monitor.SerializeCells(all) 514 coll := monitor.SerializeCells(all)
515 sort.Sort(sortableDataSlice(coll.Data)) 515 sort.Sort(sortableDataSlice(coll.Data))
516 » » » » » So(coll.Data[0].Task.ServiceName, Should Equal, 516 » » » » » So(coll.Data[0].Task.GetServiceName(), S houldEqual,
517 » » » » » » s.DefaultTarget().(*target.Task) .AsProto().ServiceName) 517 » » » » » » s.DefaultTarget().(*target.Task) .AsProto().GetServiceName())
518 » » » » » So(coll.Data[1].Task.ServiceName, Should Equal, t.AsProto().ServiceName) 518 » » » » » So(coll.Data[1].Task.GetServiceName(), S houldEqual, t.AsProto().GetServiceName())
519 }) 519 })
520 } 520 }
521 }) 521 })
522 }) 522 })
523 523
524 Convey("GetAll", t, func() { 524 Convey("GetAll", t, func() {
525 ctx, tc := testclock.UseTime(context.Background(), testclock.Tes tTimeLocal) 525 ctx, tc := testclock.UseTime(context.Background(), testclock.Tes tTimeLocal)
526 526
527 s := opts.Factory() 527 s := opts.Factory()
528 foo := &FakeMetric{"foo", "", []field.Field{}, types.NonCumulati veIntType} 528 foo := &FakeMetric{"foo", "", []field.Field{}, types.NonCumulati veIntType}
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 }) 654 })
655 655
656 Convey("Different targets", t, func() { 656 Convey("Different targets", t, func() {
657 Convey("Gets from context", func() { 657 Convey("Gets from context", func() {
658 s := opts.Factory() 658 s := opts.Factory()
659 m := &FakeMetric{"m", "", []field.Field{}, types.NonCumu lativeIntType} 659 m := &FakeMetric{"m", "", []field.Field{}, types.NonCumu lativeIntType}
660 s.Register(m) 660 s.Register(m)
661 opts.RegistrationFinished(s) 661 opts.RegistrationFinished(s)
662 662
663 t := target.Task{} 663 t := target.Task{}
664 » » » t.AsProto().ServiceName = "foo" 664 » » » t.AsProto().ServiceName = proto.String("foo")
665 ctxWithTarget := target.Set(ctx, &t) 665 ctxWithTarget := target.Set(ctx, &t)
666 666
667 So(s.Set(ctx, m, time.Time{}, []interface{}{}, int64(42) ), ShouldBeNil) 667 So(s.Set(ctx, m, time.Time{}, []interface{}{}, int64(42) ), ShouldBeNil)
668 So(s.Set(ctxWithTarget, m, time.Time{}, []interface{}{}, int64(43)), ShouldBeNil) 668 So(s.Set(ctxWithTarget, m, time.Time{}, []interface{}{}, int64(43)), ShouldBeNil)
669 669
670 val, err := s.Get(ctx, m, time.Time{}, []interface{}{}) 670 val, err := s.Get(ctx, m, time.Time{}, []interface{}{})
671 So(err, ShouldBeNil) 671 So(err, ShouldBeNil)
672 So(val, ShouldEqual, 42) 672 So(val, ShouldEqual, 42)
673 673
674 val, err = s.Get(ctxWithTarget, m, time.Time{}, []interf ace{}{}) 674 val, err = s.Get(ctxWithTarget, m, time.Time{}, []interf ace{}{})
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 721
722 type sortableDataSlice []*pb.MetricsData 722 type sortableDataSlice []*pb.MetricsData
723 723
724 func (s sortableDataSlice) Len() int { return len(s) } 724 func (s sortableDataSlice) Len() int { return len(s) }
725 func (s sortableDataSlice) Less(i, j int) bool { 725 func (s sortableDataSlice) Less(i, j int) bool {
726 a, _ := proto.Marshal(s[i]) 726 a, _ := proto.Marshal(s[i])
727 b, _ := proto.Marshal(s[j]) 727 b, _ := proto.Marshal(s[j])
728 return bytes.Compare(a, b) > 0 728 return bytes.Compare(a, b) > 0
729 } 729 }
730 func (s sortableDataSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 730 func (s sortableDataSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
OLDNEW
« no previous file with comments | « common/tsmon/store/inmemory_test.go ('k') | common/tsmon/target/target.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698