From 9907482b66c7d5ae0d7889adbb90a499e7b3cbdb Mon Sep 17 00:00:00 2001 From: seanavery Date: Mon, 30 Sep 2024 14:09:21 -0400 Subject: [PATCH] Add realtime async save test --- tests/config_test.go | 35 +++++++++++++++++++++++++++-------- tests/fetch_test.go | 4 ++-- tests/save_test.go | 42 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/tests/config_test.go b/tests/config_test.go index 42608ed..ea1dc5e 100644 --- a/tests/config_test.go +++ b/tests/config_test.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "os" + "os/exec" "path/filepath" "testing" "time" @@ -27,6 +28,7 @@ const ( ) func setupViamServer(ctx context.Context, configStr string) (robot.Robot, error) { + cleanVideoStoreDir() logger := logging.NewLogger("video-store-module") cfg, err := config.FromReader(ctx, "default.json", bytes.NewReader([]byte(configStr)), logger) if err != nil { @@ -50,6 +52,23 @@ func getModuleBinPath() (string, error) { return fullModuleBinPath, nil } +func cleanVideoStoreDir() error { + currentDir, err := os.Getwd() + if err != nil { + return err + } + videoStoreDir := filepath.Join(currentDir, "video-storage") + err = os.Chdir(videoStoreDir) + if err != nil { + return err + } + defer os.Chdir(currentDir) // Ensure we change back to the original directory + cmd := exec.Command("git", "clean", "-fdx") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} + func TestModuleConfiguration(t *testing.T) { fullModuleBinPath, err := getModuleBinPath() if err != nil { @@ -75,8 +94,8 @@ func TestModuleConfiguration(t *testing.T) { "storage_path": "%s" }, "cam_props": { - "width": 1920, - "height": 1080, + "width": 1280, + "height": 720, "framerate": 30 }, "video": { @@ -181,8 +200,8 @@ func TestModuleConfiguration(t *testing.T) { "camera": "fake-cam-1", "sync": "data_manager-1", "cam_props": { - "width": 1920, - "height": 1080, + "width": 1280, + "height": 720, "framerate": 30 }, "video": { @@ -247,8 +266,8 @@ func TestModuleConfiguration(t *testing.T) { "storage_path": "/tmp/video-storage" }, "cam_props": { - "width": 1920, - "height": 1080, + "width": 1280, + "height": 720, "framerate": 30 }, "video": { @@ -375,8 +394,8 @@ func TestModuleConfiguration(t *testing.T) { "storage_path": "/tmp" }, "cam_props": { - "width": 1920, - "height": 1080, + "width": 1280, + "height": 720, "framerate": 30 }, "video": { diff --git a/tests/fetch_test.go b/tests/fetch_test.go index d047d3f..e473875 100644 --- a/tests/fetch_test.go +++ b/tests/fetch_test.go @@ -40,8 +40,8 @@ func TestFetchDoCommand(t *testing.T) { "storage_path": "%s" }, "cam_props": { - "width": 1920, - "height": 1080, + "width": 1280, + "height": 720, "framerate": 30 }, "video": { diff --git a/tests/save_test.go b/tests/save_test.go index 3cc7faa..ab61dfe 100644 --- a/tests/save_test.go +++ b/tests/save_test.go @@ -36,13 +36,13 @@ func TestSaveDoCommand(t *testing.T) { "sync": "data_manager-1", "storage": { "size_gb": 10, - "segment_seconds": 30, + "segment_seconds": 10, "upload_path": "%s", "storage_path": "%s" }, "cam_props": { - "width": 1920, - "height": 1080, + "width": 1280, + "height": 720, "framerate": 30 }, "video": { @@ -187,9 +187,45 @@ func TestSaveDoCommand(t *testing.T) { defer cancel() r, err := setupViamServer(timeoutCtx, config1) test.That(t, err, test.ShouldBeNil) + defer r.Close(timeoutCtx) _, err = camera.FromRobot(r, videoStoreComponentName) test.That(t, err, test.ShouldBeNil) _, err = os.Stat(leftoverConcatTxtPath) test.That(t, os.IsNotExist(err), test.ShouldBeTrue) }) + + t.Run("Test Async Save DoCommand from most recent video segment", func(t *testing.T) { + timeoutCtx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + r, err := setupViamServer(timeoutCtx, config1) + test.That(t, err, test.ShouldBeNil) + defer r.Close(timeoutCtx) + vs, err := camera.FromRobot(r, videoStoreComponentName) + test.That(t, err, test.ShouldBeNil) + // Wait for the video segment to be created. + time.Sleep(10 * time.Second) + now := time.Now() + fromTime := now.Add(-5 * time.Second) + toTime := now + fromTimeStr := fromTime.Format("2006-01-02_15-04-05") + toTimeStr := toTime.Format("2006-01-02_15-04-05") + fmt.Printf("from: %s, to: %s\n", fromTimeStr, toTimeStr) + saveCmdNow := map[string]interface{}{ + "command": "save", + "from": fromTimeStr, + "to": toTimeStr, + "metadata": "test-metadata", + "async": true, + } + res, err := vs.DoCommand(timeoutCtx, saveCmdNow) + test.That(t, err, test.ShouldBeNil) + _, ok := res["filename"].(string) + test.That(t, ok, test.ShouldBeTrue) + // Wait for async save to complete. + time.Sleep(15 * time.Second) + filename := fmt.Sprintf("%s_%s_%s.mp4", videoStoreComponentName, fromTimeStr, "test-metadata") + concatPath := filepath.Join(testUploadPath, filename) + _, err = os.Stat(concatPath) + test.That(t, err, test.ShouldBeNil) + }) }