0d6162c8a put every plugins.json change under the file's lock, but
SetPageOptions writes a page-only plugin's options itself, under listMu
alone, so another magpie's save meanwhile was still lost there. It now
takes listFileMu before listMu, as SetOptions does.
Run (mote, darwin/arm64):
- go test ./internal/plugin -run Meanwhile -v: the three Meanwhile tests ok
- page.go reverted with the test kept: TestPluginPageOptionsKeepWhatAnotherMagpieSetMeanwhile
red ("SetPageOptions went on while another magpie held plugins.json's
lock"); restored, green
- full suite (go test -tags nogui ./... -timeout 45m, mote) with this
stack at b566b9d3c on 0b8c4bfc3: every package ok but two reds of that
base, both fixed on main since: internal/cli
TestEveryRouteHasACommandOrIsNamed (4c86b0ab4) and internal/tui
TestEveryWordHasZh "permission mode" (e9fcc6229)
- rebased on f1c9014cd: go vet (darwin, linux nogui, windows) and
go test of internal/gateway, internal/plugin, internal/cli and
internal/tui at the pushed head: ok (gateway 899s, plugin 158s, cli, tui)
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./..., darwin/linux/windows builds: ok at b566b9d3c
Refs #4666
179 lines
5.0 KiB
Go
179 lines
5.0 KiB
Go
package plugin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/filelock"
|
|
)
|
|
|
|
// Two magpies — the gateway that runs all day, and one run from a
|
|
// terminal (`magpie plugin options …`, the plugin manager) — write
|
|
// plugins.json from separate processes, and the in-process listMu never
|
|
// saw the other one (#4666): the later whole-list write went out with
|
|
// what the other had saved since its read. A change is now made under
|
|
// the file's lock, so the one waiting reads what the other wrote before
|
|
// it goes on, and both changes stand.
|
|
|
|
func TestPluginOptionsKeepWhatAnotherMagpieSetMeanwhile(t *testing.T) {
|
|
storeSandbox(t)
|
|
writeList(t, `{"plugins":[{"spec":"p-a"},{"spec":"p-b","options":{"x":1}}]}`)
|
|
unlock, held := filelock.Lock(listPath()+".lock", filelock.Wait)
|
|
if !held {
|
|
t.Skip("this file system holds no locks")
|
|
}
|
|
defer unlock()
|
|
done := make(chan error, 1)
|
|
go func() { done <- SetOptions("p-a", map[string]any{"ours": true}) }()
|
|
time.Sleep(300 * time.Millisecond)
|
|
select {
|
|
case err := <-done:
|
|
t.Fatalf("SetOptions went on while another magpie held plugins.json's lock (err %v)", err)
|
|
default:
|
|
}
|
|
// the other magpie's save lands meanwhile: its own options for p-b
|
|
other := Load()
|
|
for i := range other.Plugins {
|
|
if other.Plugins[i].Spec == "p-b" {
|
|
other.Plugins[i].Options = map[string]any{"theirs": float64(1)}
|
|
}
|
|
}
|
|
if err := save(other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
unlock()
|
|
if err := <-done; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l := Load()
|
|
var a, b *Entry
|
|
for i := range l.Plugins {
|
|
switch l.Plugins[i].Spec {
|
|
case "p-a":
|
|
a = &l.Plugins[i]
|
|
case "p-b":
|
|
b = &l.Plugins[i]
|
|
}
|
|
}
|
|
if a == nil || a.Options["ours"] != true {
|
|
t.Errorf("p-a's options = %v, want ours kept", a)
|
|
}
|
|
if b == nil || b.Options["theirs"] != float64(1) {
|
|
t.Errorf("p-b's options = %v, want what the other magpie saved kept", b)
|
|
}
|
|
}
|
|
|
|
func TestPluginPreferKeepsWhatAnotherMagpieSetMeanwhile(t *testing.T) {
|
|
storeSandbox(t)
|
|
writeList(t, `{"plugins":[{"spec":"p-a"},{"spec":"p-b"}]}`)
|
|
unlock, held := filelock.Lock(listPath()+".lock", filelock.Wait)
|
|
if !held {
|
|
t.Skip("this file system holds no locks")
|
|
}
|
|
defer unlock()
|
|
done := make(chan error, 1)
|
|
go func() { done <- Prefer("p-a", "prov") }()
|
|
time.Sleep(300 * time.Millisecond)
|
|
select {
|
|
case err := <-done:
|
|
t.Fatalf("Prefer went on while another magpie held plugins.json's lock (err %v)", err)
|
|
default:
|
|
}
|
|
// the other magpie's save lands meanwhile: p-b turned off
|
|
other := Load()
|
|
for i := range other.Plugins {
|
|
if other.Plugins[i].Spec == "p-b" {
|
|
other.Plugins[i].Off = true
|
|
}
|
|
}
|
|
if err := save(other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
unlock()
|
|
if err := <-done; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l := Load()
|
|
var a, b *Entry
|
|
for i := range l.Plugins {
|
|
switch l.Plugins[i].Spec {
|
|
case "p-a":
|
|
a = &l.Plugins[i]
|
|
case "p-b":
|
|
b = &l.Plugins[i]
|
|
}
|
|
}
|
|
if a == nil || l.Prefer["prov"] != "p-a" {
|
|
t.Errorf("prefer = %v, want p-a kept", l.Prefer)
|
|
}
|
|
if b == nil || !b.Off {
|
|
t.Errorf("p-b off = %v, want what the other magpie saved kept", b)
|
|
}
|
|
}
|
|
|
|
// A plugin that is only a page has its options set by SetPageOptions
|
|
// itself, not through SetOptions: it is under plugins.json's lock too.
|
|
func TestPluginPageOptionsKeepWhatAnotherMagpieSetMeanwhile(t *testing.T) {
|
|
storeSandbox(t)
|
|
dir := filepath.Join(t.TempDir(), "page-only")
|
|
if err := os.MkdirAll(filepath.Join(dir, "page"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "page", "index.html"), []byte("<p>hi</p>"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"name":"page-only","magpie":{"page":{"title":"P","file":"page/index.html"}}}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w, only := Window(dir); w == nil || !only {
|
|
t.Fatalf("the fixture is no page-only plugin: %+v only=%v", w, only)
|
|
}
|
|
spec, _ := json.Marshal(dir)
|
|
writeList(t, `{"plugins":[{"spec":`+string(spec)+`},{"spec":"p-b","options":{"x":1}}]}`)
|
|
unlock, held := filelock.Lock(listPath()+".lock", filelock.Wait)
|
|
if !held {
|
|
t.Skip("this file system holds no locks")
|
|
}
|
|
defer unlock()
|
|
done := make(chan error, 1)
|
|
go func() { done <- SetPageOptions(dir, map[string]any{"ours": true}) }()
|
|
time.Sleep(300 * time.Millisecond)
|
|
select {
|
|
case err := <-done:
|
|
t.Fatalf("SetPageOptions went on while another magpie held plugins.json's lock (err %v)", err)
|
|
default:
|
|
}
|
|
other := Load()
|
|
for i := range other.Plugins {
|
|
if other.Plugins[i].Spec == "p-b" {
|
|
other.Plugins[i].Options = map[string]any{"theirs": float64(1)}
|
|
}
|
|
}
|
|
if err := save(other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
unlock()
|
|
if err := <-done; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l := Load()
|
|
var a, b *Entry
|
|
for i := range l.Plugins {
|
|
switch l.Plugins[i].Spec {
|
|
case dir:
|
|
a = &l.Plugins[i]
|
|
case "p-b":
|
|
b = &l.Plugins[i]
|
|
}
|
|
}
|
|
if a == nil || a.Options["ours"] != true {
|
|
t.Errorf("the page plugin's options = %v, want ours kept", a)
|
|
}
|
|
if b == nil || b.Options["theirs"] != float64(1) {
|
|
t.Errorf("p-b's options = %v, want what the other magpie saved kept", b)
|
|
}
|
|
}
|