2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-06-04 11:02:39 +00:00
|
|
|
package testutil
|
2021-06-21 18:22:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWait_WaitForFilesUntil(t *testing.T) {
|
|
|
|
|
2022-01-18 18:17:37 +00:00
|
|
|
N := 10
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tmpDir := t.TempDir()
|
2021-06-21 18:22:49 +00:00
|
|
|
|
2022-01-18 18:17:37 +00:00
|
|
|
var files []string
|
|
|
|
for i := 1; i < N; i++ {
|
|
|
|
files = append(files, filepath.Join(
|
|
|
|
tmpDir, fmt.Sprintf("test%d.txt", i),
|
|
|
|
))
|
2021-06-21 18:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2022-01-18 18:17:37 +00:00
|
|
|
for _, file := range files {
|
|
|
|
t.Logf("Creating file %s ...", file)
|
|
|
|
fh, createErr := os.Create(file)
|
|
|
|
require.NoError(t, createErr)
|
2021-06-21 18:22:49 +00:00
|
|
|
|
2022-01-18 18:17:37 +00:00
|
|
|
closeErr := fh.Close()
|
|
|
|
require.NoError(t, closeErr)
|
|
|
|
require.FileExists(t, file)
|
2021-06-21 18:22:49 +00:00
|
|
|
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
duration := 5 * time.Second
|
2022-01-18 18:17:37 +00:00
|
|
|
t.Log("Waiting 5 seconds for files ...")
|
2021-06-21 18:22:49 +00:00
|
|
|
WaitForFilesUntil(t, files, duration)
|
|
|
|
}
|