Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
// Package wait is how MyGo's tests wait for what happens elsewhere: on
|
|
// the main thread, in another goroutine or process, in the window system
|
|
// or the file system. They wait until it happened, never for a time.
|
|
//
|
|
// A test that sleeps and then checks, or that gives up after a few
|
|
// seconds, decides on the speed of the machine: a busy CI runner makes it
|
|
// fail although the code is right, and a longer timeout only makes that
|
|
// rarer. Here a wait ends when its condition holds. Guard bounds it only
|
|
// so that a hang fails the test with what never happened rather than
|
|
// stalling the whole run, and it is not to be tuned: nothing a test
|
|
// waits for takes that long when the code is right. A wait that reaches
|
|
// it found a bug (an event lost to a race, a deadlock, a condition that
|
|
// can't become true), and the fix is in the code or the condition.
|
|
package wait
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Guard is the longest a wait waits before it fails the test.
|
|
const Guard = time.Minute
|
|
|
|
// Until waits until cond returns true, calling it again and again: right
|
|
// away, then at intervals growing to 20 ms. what says what is waited
|
|
// for, as "the window to close".
|
|
func Until(t testing.TB, what string, cond func() bool) {
|
|
t.Helper()
|
|
if !Poll(cond) {
|
|
t.Fatalf("%s never happened (waited %v)", what, Guard)
|
|
}
|
|
}
|
|
|
|
// Poll is Until without a test: it reports whether cond became true
|
|
// before Guard, for helpers that say more when it did not.
|
|
func Poll(cond func() bool) bool {
|
|
deadline := time.Now().Add(Guard)
|
|
pause := time.Millisecond
|
|
for !cond() {
|
|
if time.Now().After(deadline) {
|
|
return false
|
|
}
|
|
time.Sleep(pause)
|
|
pause = min(2*pause, 20*time.Millisecond)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// For waits for a value from ch.
|
|
func For[T any](t testing.TB, what string, ch <-chan T) T {
|
|
t.Helper()
|
|
select {
|
|
case v := <-ch:
|
|
return v
|
|
case <-time.After(Guard):
|
|
t.Fatalf("%s never happened (waited %v)", what, Guard)
|
|
var zero T
|
|
return zero
|
|
}
|
|
}
|
|
|
|
// Context returns a context for calls that wait on one, as
|
|
// Page.EvalContext and FileWatcher.Next do, canceled after Guard or when
|
|
// the test ends.
|
|
func Context(t testing.TB) context.Context {
|
|
ctx, cancel := context.WithTimeout(t.Context(), Guard)
|
|
t.Cleanup(cancel)
|
|
return ctx
|
|
}
|