github.com/mithrandie/csvq@v1.18.1/lib/file/functions_test.go (about) 1 package file 2 3 import ( 4 "context" 5 "regexp" 6 "testing" 7 "time" 8 ) 9 10 func TestGetTimeoutContext(t *testing.T) { 11 ctx := context.Background() 12 result, cancel := GetTimeoutContext(ctx, 10*time.Second) 13 tm1, ok := result.Deadline() 14 if !ok { 15 t.Fatalf("deadline of context does not set") 16 } 17 18 result2, _ := GetTimeoutContext(result, 100*time.Second) 19 tm2, ok := result2.Deadline() 20 if !ok { 21 t.Fatalf("deadline of context does not set") 22 } 23 if !tm1.Equal(tm2) { 24 t.Fatalf("deadline of context is changed") 25 } 26 27 cancel() 28 result3, _ := GetTimeoutContext(result2, 100*time.Second) 29 if result3.Err() == nil { 30 t.Fatalf("context is not done") 31 } 32 } 33 34 func TestRLockFilePath(t *testing.T) { 35 path := GetTestFilePath("testfile.txt") 36 result := RLockFilePath(path) 37 expect := GetTestFilePath(".testfile.txt" + ".[0-9a-zA-Z]{12}" + RLockFileSuffix) 38 r := regexp.MustCompile(expect) 39 if !r.MatchString(result) { 40 t.Errorf("result = %q, want %q", result, expect) 41 } 42 } 43 44 func TestLockFilePath(t *testing.T) { 45 path := GetTestFilePath("testfile.txt") 46 result := LockFilePath(path) 47 expect := GetTestFilePath(".testfile.txt" + LockFileSuffix) 48 if result != expect { 49 t.Errorf("result = %q, want %q", result, expect) 50 } 51 } 52 53 func TestTempFilePath(t *testing.T) { 54 path := GetTestFilePath("testfile.txt") 55 result := TempFilePath(path) 56 expect := GetTestFilePath(".testfile.txt" + TempFileSuffix) 57 if result != expect { 58 t.Errorf("result = %q, want %q", result, expect) 59 } 60 }