@@ -414,72 +414,84 @@ func TestPushRecord(t *testing.T) {
414414 }
415415 })
416416
417- //! TEST DOES NOT WORK IF WE MONITOR THE SAME VARIABLE ACROSS MULTIPLE BOARDS
418- t .Run ("With allowed variables (multiple boards)" , func (t * testing.T ) {
419- logger := createLoggerForTest (t )
420- logger .Start ()
417+ t .Run ("With allowed variables (multiple boards, different values Name)" , func (t * testing.T ) {
418+ multipleBoardsValueNames (t , []string {"def" , "def2" })
419+ })
421420
422- //variables
421+ // The same as before but the loggers have the same value Name
422+ t .Run ("With allowed variables (multiple boards, same value Name)" , func (t * testing.T ) {
423423
424- values := []string {"def" , "def2" }
424+ multipleBoardsValueNames ( t , []string {"def" , "def" })
425425
426- // values to log per board
427- vals := map [string ]float64 {
428- "Pikachu" : 11.11 ,
429- "Bulbasaur" : 22.22 ,
430- }
426+ })
431427
432- // push one record per board using "def" and unique packet IDs
433- var id abstraction.PacketId = 1
434- for b , v := range vals {
435- pkt := data .NewPacket (id ).
436- SetValue (data .ValueName (values [id - 1 ]), data .NewNumericValue (v ), true )
437- id ++
438-
439- rec := Record {
440- Packet : pkt ,
441- From : b ,
442- To : "Logger" ,
443- Timestamp : time .Now (),
444- }
428+ })
445429
446- if err := logger .PushRecord (& rec ); err != nil {
447- t .Fatalf ("PushRecord failed for board %s: %v" , b , err )
448- }
449- }
430+ }
450431
451- // esperar a que terminen las escrituras asíncronas
452- time .Sleep (2 * time .Second )
432+ // Test Multiple boards with the valueNames got from input
453433
454- i := 0
434+ func multipleBoardsValueNames ( t * testing. T , values [] string ) {
455435
456- // comprobar ficheros y valores
457- for b , v := range vals {
458- filePath := filepath .Join ("logger" , loggerHandler .Timestamp .Format (loggerHandler .TimestampFormat ), "data" , strings .ToUpper (b ), values [i ]+ ".csv" )
459- i ++
460- if _ , err := os .Stat (filePath ); err != nil {
461- t .Fatalf ("expected file %s to exist, but got error: %v" , filePath , err )
462- }
436+ logger := createLoggerForTest (t )
437+ logger .Start ()
463438
464- bts , err := os .ReadFile (filePath )
465- if err != nil {
466- t .Fatalf ("could not read file %s: %v" , filePath , err )
467- }
468- lines := strings .Split (strings .TrimSpace (string (bts )), "\n " )
469- last := strings .TrimSpace (lines [len (lines )- 1 ])
470- fields := strings .Split (last , "," )
471- if len (fields ) < 1 {
472- t .Fatalf ("unexpected CSV content in %s: %q" , filePath , last )
473- }
474- got := fields [len (fields )- 1 ]
475- want := strconv .FormatFloat (v , 'f' , - 1 , 64 )
476- if got != want {
477- t .Fatalf ("unexpected value in %s: got %q, want %q" , filePath , got , want )
478- }
479- }
480- })
481- })
439+ //variables
440+
441+ // values to log per board
442+ vals := map [string ]float64 {
443+ "Pikachu" : 11.11 ,
444+ "Bulbasaur" : 22.22 ,
445+ }
446+
447+ // push one record per board using "def" and unique packet IDs
448+ var id abstraction.PacketId = 1
449+ for b , v := range vals {
450+ pkt := data .NewPacket (id ).
451+ SetValue (data .ValueName (values [id - 1 ]), data .NewNumericValue (v ), true )
452+ id ++
482453
454+ rec := Record {
455+ Packet : pkt ,
456+ From : b ,
457+ To : "Logger" ,
458+ Timestamp : time .Now (),
459+ }
460+
461+ if err := logger .PushRecord (& rec ); err != nil {
462+ t .Fatalf ("PushRecord failed for board %s: %v" , b , err )
463+ }
464+ }
465+
466+ // esperar a que terminen las escrituras asíncronas
467+ time .Sleep (2 * time .Second )
468+
469+ i := 0
470+
471+ // comprobar ficheros y valores
472+ for b , v := range vals {
473+ filePath := filepath .Join ("logger" , loggerHandler .Timestamp .Format (loggerHandler .TimestampFormat ), "data" , strings .ToUpper (b ), values [i ]+ ".csv" )
474+ i ++
475+ if _ , err := os .Stat (filePath ); err != nil {
476+ t .Fatalf ("expected file %s to exist, but got error: %v" , filePath , err )
477+ }
478+
479+ bts , err := os .ReadFile (filePath )
480+ if err != nil {
481+ t .Fatalf ("could not read file %s: %v" , filePath , err )
482+ }
483+ lines := strings .Split (strings .TrimSpace (string (bts )), "\n " )
484+ last := strings .TrimSpace (lines [len (lines )- 1 ])
485+ fields := strings .Split (last , "," )
486+ if len (fields ) < 1 {
487+ t .Fatalf ("unexpected CSV content in %s: %q" , filePath , last )
488+ }
489+ got := fields [len (fields )- 1 ]
490+ want := strconv .FormatFloat (v , 'f' , - 1 , 64 )
491+ if got != want {
492+ t .Fatalf ("unexpected value in %s: got %q, want %q" , filePath , got , want )
493+ }
494+ }
483495}
484496
485497/*************
0 commit comments