Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JackKCWong committed Mar 3, 2022
1 parent 8f1822f commit 8ebf630
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
34 changes: 34 additions & 0 deletions fuzzyfinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,40 @@ func TestFind_hotReload(t *testing.T) {
})
}

func TestFind_hotReloadLock(t *testing.T) {
f, term := fuzzyfinder.NewWithMockedTerminal()
events := append(runes("adrena"), keys(input{tcell.KeyEsc, rune(tcell.KeyEsc), tcell.ModNone})...)
term.SetEventsV2(events...)

var mu sync.RWMutex
assertWithGolden(t, func(t *testing.T) string {
_, err := f.Find(
&tracks,
func(i int) string {
return tracks[i].Name
},
fuzzyfinder.WithPreviewWindow(func(i, width, height int) string {
// Hack, wait until updateItems is called.
time.Sleep(50 * time.Millisecond)
mu.RLock()
defer mu.RUnlock()
if i == -1 {
return "not found"
}
return "Name: " + tracks[i].Name + "\nArtist: " + tracks[i].Artist
}),
fuzzyfinder.WithMode(fuzzyfinder.ModeCaseSensitive),
fuzzyfinder.WithHotReloadLock(mu.RLocker()),
)
if !errors.Is(err, fuzzyfinder.ErrAbort) {
t.Fatalf("Find must return ErrAbort, but got '%s'", err)
}

res := term.GetResult()
return res
})
}

func TestFind_enter(t *testing.T) {
cases := map[string]struct {
events []tcell.Event
Expand Down
3 changes: 3 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (

var defaultOption = opt{
promptString: "> ",
hotReloadLock: &sync.Mutex{}, // this won't resolve the race condition but avoid nil panic
}

// Option represents available fuzzy-finding options.
Expand Down Expand Up @@ -66,7 +67,9 @@ func WithHotReload() Option {

// WithHotReloadLock reloads the passed slice automatically when some entries are appended.
// The caller must pass a pointer of the slice instead of the slice itself.
// The caller must pass a RLock which is used to synchronize access to the slice.
// The caller MUST NOT lock in the itemFunc passed to Find / FindMulti because it will be locked by the fuzzyfinder.
// If used together with WithPreviewWindow, the caller MUST use the RLock only in the previewFunc passed to WithPreviewWindow.
func WithHotReloadLock(lock sync.Locker) Option {
return func(o *opt) {
o.hotReload = true
Expand Down
11 changes: 11 additions & 0 deletions testdata/fixtures/testfind_hotreloadlock.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
┌────────────────────────────┐
│ Name: adrenaline!!! │
│ Artist: TrySail │
│ │
│ │
│ │
│ │
> adrenaline!!! │ │
1/9 │ │
> adrena█ └────────────────────────────┘


0 comments on commit 8ebf630

Please # to comment.