Skip to content

Commit

Permalink
Add realtime async save test
Browse files Browse the repository at this point in the history
  • Loading branch information
seanavery committed Sep 30, 2024
1 parent 1b1af1b commit 9907482
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
35 changes: 27 additions & 8 deletions tests/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions tests/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
42 changes: 39 additions & 3 deletions tests/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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)

Check failure on line 212 in tests/save_test.go

View workflow job for this annotation

GitHub Actions / quality-checks

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
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)
})
}

0 comments on commit 9907482

Please sign in to comment.