Files
openchamber-android-native/third_party/mygo/appfile_test.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
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.
2026-11-18 13:22:59 +08:00

90 lines
2.5 KiB
Go

package mygo
import (
"io"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
// TestAppFileReadDoesNotBlockReplace: a file of the app, open as
// readAppFile opens it, is replaced at once, without waiting for the
// reader to let go, which goes on reading what it opened.
func TestAppFileReadDoesNotBlockReplace(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "index.json")
if err := os.WriteFile(path, []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
f, err := openShared(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
tmp := filepath.Join(dir, ".index.json.tmp")
if err := os.WriteFile(tmp, []byte("new"), 0o644); err != nil {
t.Fatal(err)
}
// renameOver, not replaceAppFile, which would retry until f is closed.
if err := renameOver(tmp, path); err != nil {
t.Fatalf("replacing a file the app reads: %v", err)
}
if b, err := io.ReadAll(f); err != nil || string(b) != "old" {
t.Errorf("read %q, %v from the file opened before the replace", b, err)
}
if b, err := readAppFile(path); err != nil || string(b) != "new" {
t.Errorf("read %q, %v after the replace", b, err)
}
}
// TestAppFileInUse: a file of the app that another program has open, as
// antivirus software scanning it, is replaced once it lets go, and read
// while it is being replaced.
func TestAppFileInUse(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("only Windows keeps open files from being replaced")
}
path := filepath.Join(t.TempDir(), "index.json")
if err := os.WriteFile(path, []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
// Go opens files without sharing their deletion, which replacing needs.
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { f.Close() })
time.AfterFunc(500*time.Millisecond, func() { f.Close() })
if err := writeAppFile(path, func(w io.Writer) error { _, err := io.WriteString(w, "new"); return err }); err != nil {
t.Fatal(err)
}
if b, err := readAppFile(path); err != nil || string(b) != "new" {
t.Errorf("read %q, %v after the replace", b, err)
}
// Reads while it is replaced over and over.
done := make(chan struct{})
go func() {
defer close(done)
for i := 0; i < 200; i++ {
if err := writeAppFile(path, func(w io.Writer) error { _, err := io.WriteString(w, "x"); return err }); err != nil {
t.Errorf("write %d: %v", i, err)
return
}
}
}()
for {
select {
case <-done:
return
default:
}
if _, err := readAppFile(path); err != nil {
t.Errorf("read while replaced: %v", err)
<-done
return
}
}
}