From cc0789df928d21c5bea9f06d85adf341ca5136f5 Mon Sep 17 00:00:00 2001 From: Intizar Date: Fri, 23 Aug 2024 17:03:01 +0900 Subject: [PATCH] remove setting reporters in TestWsDataHandling --- node/pkg/reporter/app_test.go | 122 +++++++++++++++++----------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/node/pkg/reporter/app_test.go b/node/pkg/reporter/app_test.go index 0c3ba69f1..228eb1e79 100644 --- a/node/pkg/reporter/app_test.go +++ b/node/pkg/reporter/app_test.go @@ -4,6 +4,11 @@ package reporter import ( "context" "testing" + "time" + + "bisonai.com/miko/node/pkg/common/keys" + "bisonai.com/miko/node/pkg/dal/hub" + "bisonai.com/miko/node/pkg/db" ) func TestFetchConfigs(t *testing.T) { @@ -23,72 +28,67 @@ func TestRunApp(t *testing.T) { } } -// func TestWsDataHandling(t *testing.T) { -// ctx := context.Background() - -// app := New() +func TestWsDataHandling(t *testing.T) { + ctx := context.Background() -// err := app.setReporters(ctx) -// if err != nil { -// t.Fatalf("error setting reporters: %v", err) -// } + app := New() -// conn, tmpConfig, configs, err := mockDalWsServer(ctx) -// if err != nil { -// t.Fatalf("error mocking dal ws server: %v", err) -// } + conn, tmpConfig, configs, err := mockDalWsServer(ctx) + if err != nil { + t.Fatalf("error mocking dal ws server: %v", err) + } -// app.WsHelper = conn -// go app.WsHelper.Run(ctx, app.HandleWsMessage) -// time.Sleep(100 * time.Millisecond) + app.WsHelper = conn + go app.WsHelper.Run(ctx, app.HandleWsMessage) + time.Sleep(100 * time.Millisecond) -// err = conn.Write(ctx, hub.Subscription{ -// Method: "SUBSCRIBE", -// Params: []string{"submission@test-aggregate"}, -// }) -// if err != nil { -// t.Fatalf("error subscribing to websocket: %v", err) -// } + err = conn.Write(ctx, hub.Subscription{ + Method: "SUBSCRIBE", + Params: []string{"submission@test-aggregate"}, + }) + if err != nil { + t.Fatalf("error subscribing to websocket: %v", err) + } -// sampleSubmissionData, err := generateSampleSubmissionData( -// tmpConfig.ID, -// int64(15), -// time.Now(), -// 1, -// "test-aggregate", -// ) -// if err != nil { -// t.Fatalf("error generating sample submission data: %v", err) -// } + sampleSubmissionData, err := generateSampleSubmissionData( + tmpConfig.ID, + int64(15), + time.Now(), + 1, + "test-aggregate", + ) + if err != nil { + t.Fatalf("error generating sample submission data: %v", err) + } -// err = db.Publish(ctx, keys.SubmissionDataStreamKey(sampleSubmissionData.GlobalAggregate.ConfigID), sampleSubmissionData) -// if err != nil { -// t.Fatalf("error publishing sample submission data: %v", err) -// } + err = db.Publish(ctx, keys.SubmissionDataStreamKey(sampleSubmissionData.GlobalAggregate.ConfigID), sampleSubmissionData) + if err != nil { + t.Fatalf("error publishing sample submission data: %v", err) + } -// ticker := time.NewTicker(1 * time.Second) -// defer ticker.Stop() -// timeout := time.After(10 * time.Second) -// submissionDataCount := 0 + ticker := time.NewTicker(1 * time.Second) + defer ticker.Stop() + timeout := time.After(10 * time.Second) + submissionDataCount := 0 -// for { -// select { -// case <-ticker.C: -// if app.WsHelper != nil && app.WsHelper.IsRunning { -// submissionDataCount = 0 -// for _, config := range configs { -// if _, ok := app.LatestDataMap.Load(config.Name); ok { -// submissionDataCount++ -// } -// } -// if submissionDataCount == len(configs) { -// return -// } -// } -// case <-timeout: -// if submissionDataCount != len(configs) { -// t.Fatal("not all submission data received from websocket") -// } -// } -// } -// } + for { + select { + case <-ticker.C: + if app.WsHelper != nil && app.WsHelper.IsRunning { + submissionDataCount = 0 + for _, config := range configs { + if _, ok := app.LatestDataMap.Load(config.Name); ok { + submissionDataCount++ + } + } + if submissionDataCount == len(configs) { + return + } + } + case <-timeout: + if submissionDataCount != len(configs) { + t.Fatal("not all submission data received from websocket") + } + } + } +}