1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-26 23:10:42 +02:00

сделал Pause_ctx()

This commit is contained in:
Nikitin Aleksandr
2024-09-27 10:04:44 +03:00
parent 24eb1a275f
commit 06299c6ecb
2 changed files with 38 additions and 0 deletions

View File

@@ -91,6 +91,17 @@ func Pause(ms int) {
Sleep(ms)
}
// Pause_ctx - приостановка работы программы на нужное число миллисекунд, с учётом глобального контекста
func Pause_ctx(ctx context.Context, ms int) {
Duration := time.Duration(ms) * time.Millisecond
select {
case <-ctx.Done():
case <-time.After(Duration):
}
}
// FindDirUp - возвращает строку с именем каталога на уровень выше
func FindDirUp(dir string) string {
otvet := dir

View File

@@ -908,3 +908,30 @@ func TestInt32FromString(t *testing.T) {
t.Errorf("Expected %d, but got: %d", expected3, result3)
}
}
func TestPause_ctx(t *testing.T) {
t.Run("Context canceled before pause duration elapses", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
start := time.Now()
Pause_ctx(ctx, 100)
duration := time.Since(start)
if duration > 50*time.Millisecond {
t.Errorf("Pause_ctx did not return in a timely manner when context was canceled")
}
})
t.Run("Context not canceled before pause duration elapses", func(t *testing.T) {
ctx := context.Background()
start := time.Now()
Pause_ctx(ctx, 100)
duration := time.Since(start)
if duration < 100*time.Millisecond {
t.Errorf("Pause_ctx did not wait for the specified duration when context was not canceled")
}
})
}