Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions adb_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package adb

import (
"context"
"errors"
"net"
"reflect"
"testing"
Expand Down Expand Up @@ -238,6 +240,27 @@ func TestTapSequence_GetLength(t *testing.T) {
}
}

func TestSequenceSleep_PlayHonorsContextCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

start := time.Now()
err := (SequenceSleep{Duration: time.Second, Type: SeqSleep}).Play(Device{}, ctx)
if !errors.Is(err, context.Canceled) {
t.Fatalf("SequenceSleep.Play() error = %v, want context.Canceled", err)
}
if elapsed := time.Since(start); elapsed > 100*time.Millisecond {
t.Fatalf("SequenceSleep.Play() took %v after cancellation", elapsed)
}
}

func TestSequenceSleep_PlayCompletesAfterDuration(t *testing.T) {
err := (SequenceSleep{Duration: time.Millisecond, Type: SeqSleep}).Play(Device{}, context.Background())
if err != nil {
t.Fatalf("SequenceSleep.Play() error = %v, want nil", err)
}
}

func TestTapSequence_JSONRoundTrip(t *testing.T) {
now := time.UnixMilli(1700000000000)
original := TapSequence{
Expand Down
12 changes: 9 additions & 3 deletions capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ type SequenceSleep struct {
}

func (s SequenceSleep) Play(d Device, ctx context.Context) error {
// TODO check if context is expired
time.Sleep(s.Duration)
return nil
timer := time.NewTimer(s.Duration)
defer timer.Stop()

select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
return nil
}
}

func (s SequenceSleep) Length() time.Duration {
Expand Down
Loading