This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch eo-api-finalize
in repository ego.
View the commit online.
commit c3cc9e8c9da9b8707cb7f8ecb81ac47b18b3bf7e
Author: Cedric Bail <[email protected]>
AuthorDate: Thu Nov 26 04:30:51 2026 -0600
efl: a future's content, values and deadlines hold off the loop thread
A typed future is awaited off the loop thread, which three things did
not allow for. Content.Free dropped Eina's reference, whose count is
not atomic, on whatever goroutine called it. Await, once its context
had ended, waited for room in the loop's queue to cancel the EFL
future, so it outlived its deadline exactly when the loop was stuck.
And nothing said whose a value given to a future's settle function was,
while the copy for EFL is made later, on the loop: a producer freeing
it after settling raced with that copy. Free now posts its release,
the cancellation never waits, and a value belongs to the future once
it settles with it.
future<Eina.Error> is a Future[struct{}]: the error is the rejection,
and Await returning two errors said nothing more. An array of objects
keeps its nil entries, so a slice of children stays as long as what was
asked for. A list of objects stays a Value, as nothing converts one.
eo.WriteProperty returns the model's own future rather than a channel
fed by a goroutine that a model which never settled kept for good.
A producer that timed out reaches EFL as ETIMEDOUT and comes back as
context.DeadlineExceeded, and Rejected(nil) says what it was given.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
README.md | 5 +-
cmd/ego-gen/future.go | 7 ++-
cmd/ego-gen/future_test.go | 33 ++++++++++-
efl/README.md | 30 ++++++----
efl/content.go | 26 +++++++--
efl/content_test.go | 59 +++++++++++++++++++
efl/efl.go | 17 ++++--
efl/eo/README.md | 5 ++
efl/eo/cgo_gen_0.go | 4 +-
efl/eo/doc.go | 5 ++
efl/eo/list_model_test.go | 4 +-
efl/eo/loop_consumer.go | 2 +-
efl/eo/model_test.go | 15 ++++-
efl/eo/property.go | 37 ++++--------
efl/eo/struct_model_children_test.go | 6 +-
efl/eo/struct_model_test.go | 14 ++---
efl/eort/future.go | 13 -----
efl/error.go | 18 +++++-
efl/future.go | 51 +++++++++++-----
efl/future_test.go | 110 ++++++++++++++++++++++++++++++++++-
efl/ui/view_model_derive_test.go | 6 +-
efl/ui/view_model_logic_test.go | 67 +++++++++++++++++++--
efl/value.go | 16 +++--
examples/weather/main_test.go | 25 +++++---
24 files changed, 450 insertions(+), 125 deletions(-)
diff --git a/README.md b/README.md
index 561b2c61..54c467ce 100644
--- a/README.md
+++ b/README.md
@@ -165,7 +165,10 @@ A future of any value is an `*efl.Future[*efl.Value]`, whose value is the
caller's, and one of nothing an `*efl.Future[struct{}]`. An implementation
of such a function, such as a model's `SetProperty`, returns
`efl.Resolved(v)`, `efl.Rejected[T](err)`, or a future of `efl.NewFuture`
-that it settles later.
+that it settles later. A value a future is settled with belongs to the future
+from then on: every `Await` returns that same value, so the producer does not
+free or change it afterwards, from any goroutine. `eo.WriteProperty` writes a
+property of a model and returns the model's own `*efl.Future[*efl.Value]`.
**Models and views (MVVM).** EFL's list and grid views show an `Efl.Model`,
building only the rows on screen.
diff --git a/cmd/ego-gen/future.go b/cmd/ego-gen/future.go
index 56bab06d..449e205d 100644
--- a/cmd/ego-gen/future.go
+++ b/cmd/ego-gen/future.go
@@ -62,7 +62,9 @@ func futureOf(t *Type) futureType {
return futureType{Elem: "*efl.Object", Kind: "Object"}
}
if value.IsArray() || value.IsList() {
- if element := value.BaseType(); element != nil && element.IsClass() {
+ // Only an array of objects has a converter: ObjectsFromValue reads
+ // EINA_VALUE_TYPE_ARRAY, which a list is not.
+ if element := value.BaseType(); value.IsArray() && element != nil && element.IsClass() {
return futureType{Elem: "[]*efl.Object", Kind: "Objects"}
}
return anyFuture
@@ -79,7 +81,8 @@ func futureOfCType(ctype string) futureType {
case IsEinaValue(ctype):
return anyFuture
case IsEinaError(ctype):
- return futureType{Elem: "error", Kind: "Error"}
+ // A failure rejects the future, and has no value of its own.
+ return futureType{Elem: "struct{}", Kind: "Void"}
case IsEinaContent(ctype):
return futureType{Elem: "*efl.Content", Kind: "Content"}
}
diff --git a/cmd/ego-gen/future_test.go b/cmd/ego-gen/future_test.go
index 4dc0570f..8f32315a 100644
--- a/cmd/ego-gen/future_test.go
+++ b/cmd/ego-gen/future_test.go
@@ -167,7 +167,7 @@ func TestFutureReturnsAreTyped(t *testing.T) {
"Numbers": {"*efl.Value", "eort.AnyFromValue"},
"Area": {"image.Rectangle", "eort.RectFromValue"},
"Dragged": {"*efl.Content", "eort.ContentFromValue"},
- "Failure": {"error", "eort.ErrorFromValue"},
+ "Failure": {"struct{}", "eort.VoidFromValue"},
"Ask": {"string", "eort.ScalarFromValue[string]"},
} {
signature := "func (o *Futures) " + method + "() *efl.Future[" + future.elem + "] {"
@@ -226,6 +226,37 @@ func cgoSources(files map[string]string) []string {
return out
}
+// A future of a list of objects has no converter: ObjectsFromValue reads an
+// array, so it is left as a value for its caller to read.
+func TestFutureOfAListIsAnyValue(t *testing.T) {
+ files := generateFixtures(t, map[string]string{
+ "test_thing.eo": `
+class @beta Test.Thing {
+ [[A thing. @since 1.30]]
+ c_prefix: test_thing;
+ methods {
+ things {
+ [[A list.]]
+ return: future<list<Test.Thing>>; [[Them.]]
+ }
+ more {
+ [[An array.]]
+ return: future<array<Test.Thing>>; [[Them.]]
+ }
+ }
+}`,
+ })
+ class := files["thing.go"]
+ for _, want := range []string{
+ "func (o *Thing) Things() *efl.Future[*efl.Value] {",
+ "func (o *Thing) More() *efl.Future[[]*efl.Object] {",
+ } {
+ if !strings.Contains(class, want) {
+ t.Errorf("thing.go lacks %q:\n%s", want, class)
+ }
+ }
+}
+
// An enum a future resolves to is the Go enum, as one a function returns is.
func TestFutureOfEnum(t *testing.T) {
RegisterEnum("Test_Kind", "efl", "Kind")
diff --git a/efl/README.md b/efl/README.md
index 785e2b3c..212e09d7 100644
--- a/efl/README.md
+++ b/efl/README.md
@@ -534,9 +534,10 @@ for the error code. Returns nil when code is zero (EINA_ERROR_NO_ERROR).
NewErrorValue makes an error value carrying err, the shape EFL gives a
failure in: what a model's Property returns for a property it cannot
-give. err is carried as its Eina_Error code -- a sentinel such as ErrModelReadOnly, an
-*EinaError, or a syscall.Errno -- and any other error as EINVAL. The value
-is owned, as NewValue's is.
+give. err is carried as its Eina_Error code -- a sentinel such as
+ErrModelReadOnly, an *EinaError, a syscall.Errno, or ETIMEDOUT for
+context.DeadlineExceeded -- and any other error as EINVAL. The value is
+owned, as NewValue's is.
### NewFuture
@@ -546,12 +547,17 @@ settle may be called from any goroutine, and only its first call counts. A
non-nil err rejects the future, and the value is then ignored; a *Value
carrying an error rejects it with that error.
+A value settles the future with becomes the future's: every Await returns
+that same value, and the one EFL is given is a copy made later, on the loop
+thread. The producer must neither free nor change a *Value, or an object
+held in a value, after passing it to settle, from any goroutine; the
+garbage collector releases it once nobody holds it. Objects in the value
+must live until EFL has its copy, which a wrapper owning a reference, as
+Object.Ref returns, sees to.
+
A future an implementation hands EFL is settled with context.Canceled when
EFL cancels it, so a producer that Awaits it finds out and can stop its
-work; give each call a future of its own. Objects in the value are given to
-EFL on its thread, when settle is called there and otherwise once the loop
-gets to it: they must live until then, which a wrapper owning a reference,
-as Object.Ref returns, sees to.
+work; give each call a future of its own.
### NewHandle
@@ -642,12 +648,16 @@ Panics if called before Init.
Rejected returns a future that has already failed with err. EFL is told of
it as its Eina_Error code -- a sentinel such as ErrModelReadOnly, an
-*EinaError, a syscall.Errno, or ECANCELED for context.Canceled -- and any
-other error, nil included, as EINVAL.
+*EinaError, a syscall.Errno, ECANCELED for context.Canceled, or ETIMEDOUT
+for context.DeadlineExceeded -- and any other error as EINVAL. A nil err
+is a mistake of the caller's: Await returns an error saying so, and EFL is
+told EINVAL.
### Resolved
-Resolved returns a future that has already succeeded with value.
+Resolved returns a future that has already succeeded with value, which
+belongs to the future from now on, as NewFuture says of what settle is
+given.
### Run
diff --git a/efl/content.go b/efl/content.go
index 3937559d..c27b3340 100644
--- a/efl/content.go
+++ b/efl/content.go
@@ -31,6 +31,7 @@ import "C"
import (
"runtime"
+ "sync/atomic"
"unsafe"
)
@@ -44,6 +45,8 @@ import (
type Content struct {
ptr *C.Eina_Content
owned bool
+ // freed is set by the one Free that releases the content.
+ freed atomic.Bool
// cleanup is the free the garbage collector is to make of a Content EFL
// handed over, until Free makes it for good.
cleanup runtime.Cleanup
@@ -131,14 +134,25 @@ func (c *Content) Data() []byte {
return C.GoBytes(unsafe.Pointer(mem), C.int(length))
}
-// Free releases the underlying Eina_Content when this wrapper owns it.
-// Calling Free on a nil or non-owned Content is a no-op. After Free the
-// pointer is set to nil so subsequent calls are safe.
+// Free releases the underlying Eina_Content when this wrapper owns it, and
+// may be called from any goroutine and more than once: the content is
+// released once. EFL may hold the same Eina_Content, and its reference count
+// is not atomic, so the release is made on the EFL thread: at once when Free
+// is called there, and queued when it is not. Calling Free on a nil or
+// non-owned Content is a no-op. After Free the Content is nil, as IsNil says.
func (c *Content) Free() {
- if c == nil || c.ptr == nil || !c.owned {
+ if c == nil || !c.owned || !c.freed.CompareAndSwap(false, true) {
return
}
- c.cleanup.Stop()
- C.eina_content_free(c.ptr)
+ ptr := c.ptr
c.ptr = nil
+ c.cleanup.Stop()
+ if ptr == nil {
+ return
+ }
+ if InLoop() {
+ C.eina_content_free(ptr)
+ return
+ }
+ collectContent(unsafe.Pointer(ptr))
}
diff --git a/efl/content_test.go b/efl/content_test.go
index 23e8bca5..524415c7 100644
--- a/efl/content_test.go
+++ b/efl/content_test.go
@@ -2,7 +2,10 @@ package efl_test
import (
"bytes"
+ "sync"
+ "sync/atomic"
"testing"
+ "unsafe"
"git.enlightenment.org/cedric/ego/efl"
"git.enlightenment.org/cedric/ego/efl/eort"
@@ -137,3 +140,59 @@ func TestContentNilSafety(t *testing.T) {
t.Error("IsNil on nil Content: want true")
}
}
+
+// A Content can be freed from any goroutine, as one a future resolved with
+// is: the release is queued for the EFL thread, where EFL's own reference
+// count is touched, and made once however many goroutines free it.
+func TestContentFreeFromGoroutinesReleasesOnce(t *testing.T) {
+ c := efl.NewContent([]byte("shared\x00"), "text/plain")
+ if c == nil {
+ t.Fatal("NewContent returned nil")
+ }
+ ptr := eort.ContentPtr(c)
+ var released atomic.Int32
+ t.Cleanup(efl.ObserveCollected(func(p unsafe.Pointer, _ bool) {
+ if p == ptr {
+ released.Add(1)
+ }
+ }))
+
+ var wg sync.WaitGroup
+ for range 8 {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ c.Free()
+ }()
+ }
+ wg.Wait()
+ efl.Sync(func() {}) // the release was queued before this
+ if n := released.Load(); n != 1 {
+ t.Errorf("the content was released %d times, want 1", n)
+ }
+ if !c.IsNil() {
+ t.Error("a freed Content is not nil")
+ }
+}
+
+// Freed on the EFL thread, a Content is released there and then.
+func TestContentFreeOnTheLoopReleasesAtOnce(t *testing.T) {
+ c := efl.NewContent([]byte("here\x00"), "text/plain")
+ ptr := eort.ContentPtr(c)
+ var queued atomic.Int32
+ t.Cleanup(efl.ObserveCollected(func(p unsafe.Pointer, _ bool) {
+ if p == ptr {
+ queued.Add(1)
+ }
+ }))
+ efl.Sync(func() {
+ c.Free()
+ c.Free()
+ })
+ if n := queued.Load(); n != 0 {
+ t.Errorf("a Free on the loop queued %d releases, want none", n)
+ }
+ if !c.IsNil() {
+ t.Error("a freed Content is not nil")
+ }
+}
diff --git a/efl/efl.go b/efl/efl.go
index 9d53c213..2d768c41 100644
--- a/efl/efl.go
+++ b/efl/efl.go
@@ -333,6 +333,18 @@ func tryPost(fn func()) bool {
// For tests.
var collectedHook atomic.Pointer[func(ptr unsafe.Pointer, waited bool)]
+// postWithoutWaiting has fn run on the EFL thread, later, and never blocks the
+// caller: when the queue has no room a goroutine waits for it, as the work is
+// not to be lost to a busy loop. It reports whether it had to wait. Nothing
+// happens once EFL is shutting down, when nothing runs any more.
+func postWithoutWaiting(fn func()) (waited bool) {
+ waited = !tryPost(fn)
+ if waited && inited.Load() && !closed.Load() {
+ go PostContext(context.Background(), fn) //nolint:errcheck // fails only once EFL is shutting down
+ }
+ return waited
+}
+
// postCollected has fn, which releases the object or value at ptr that a
// collected wrapper held, run on the EFL thread. It is for the cleanups the
// garbage collector runs, which must not block it. After Shutdown there is
@@ -341,10 +353,7 @@ var collectedHook atomic.Pointer[func(ptr unsafe.Pointer, waited bool)]
// the release, as it is the only one the object or value will ever get, so a
// goroutine waits for room.
func postCollected(ptr unsafe.Pointer, fn func()) {
- waited := !tryPost(fn)
- if waited && inited.Load() && !closed.Load() {
- go PostContext(context.Background(), fn) //nolint:errcheck // fails only once EFL is shutting down, as above
- }
+ waited := postWithoutWaiting(fn)
if hook := collectedHook.Load(); hook != nil {
(*hook)(ptr, waited)
}
diff --git a/efl/eo/README.md b/efl/eo/README.md
index 53016346..7c380432 100644
--- a/efl/eo/README.md
+++ b/efl/eo/README.md
@@ -26,6 +26,11 @@ which plain widgets bind to with PropertyBind. GenericModel holds
properties set by hand, and FilterModel, SortedModel and BooleanModel wrap
another model. A model of
one's own, over a database say, is a class defined with efl.DefineClass.
+Its methods that answer later return an *efl.Future: efl.Resolved,
+efl.Rejected, or the future of efl.NewFuture the model settles from any
+goroutine. A value a future is settled with belongs to it from then on, and
+the model does not free or change it afterwards. WriteProperty writes a
+property of any model, and returns the future the model answers with.
## Shared interfaces
diff --git a/efl/eo/cgo_gen_0.go b/efl/eo/cgo_gen_0.go
index fe7cfbcc..fc19197c 100644
--- a/efl/eo/cgo_gen_0.go
+++ b/efl/eo/cgo_gen_0.go
@@ -487,8 +487,8 @@ func cEflIoReaderRead(_obj unsafe.Pointer, p []byte) (code, n int) {
return int(cErr), int(C._ego_rw_slice_len(&slice))
}
-func cEflLoopFutureRejected(_obj unsafe.Pointer, error int) *efl.Future[error] {
- return eort.FutureFromC(unsafe.Pointer(C.efl_loop_future_rejected((*C.Eo)(_obj), C.Eina_Error(error))), eort.ErrorFromValue)
+func cEflLoopFutureRejected(_obj unsafe.Pointer, error int) *efl.Future[struct{}] {
+ return eort.FutureFromC(unsafe.Pointer(C.efl_loop_future_rejected((*C.Eo)(_obj), C.Eina_Error(error))), eort.VoidFromValue)
}
func cEflLoopJob(_obj unsafe.Pointer) *efl.Future[struct{}] {
diff --git a/efl/eo/doc.go b/efl/eo/doc.go
index 8a6b1544..985b5f63 100644
--- a/efl/eo/doc.go
+++ b/efl/eo/doc.go
@@ -24,6 +24,11 @@
// properties set by hand, and FilterModel, SortedModel and BooleanModel wrap
// another model. A model of
// one's own, over a database say, is a class defined with efl.DefineClass.
+// Its methods that answer later return an *efl.Future: efl.Resolved,
+// efl.Rejected, or the future of efl.NewFuture the model settles from any
+// goroutine. A value a future is settled with belongs to it from then on, and
+// the model does not free or change it afterwards. WriteProperty writes a
+// property of any model, and returns the future the model answers with.
//
// # Shared interfaces
//
diff --git a/efl/eo/list_model_test.go b/efl/eo/list_model_test.go
index 0e32b400..958f6e63 100644
--- a/efl/eo/list_model_test.go
+++ b/efl/eo/list_model_test.go
@@ -93,10 +93,10 @@ func TestListModelIsReadOnly(t *testing.T) {
m, view := newFruits(t, fruit{Name: "apple"})
row := rows(t, view, 0, 1)[0]
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(row, "name", "plum") }); !errors.Is(err, efl.ErrModelReadOnly) {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(row, "name", "plum") })); !errors.Is(err, efl.ErrModelReadOnly) {
t.Errorf("setting a row's property gave %v, want ErrModelReadOnly", err)
}
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(row, "colour", "red") }); !errors.Is(err, efl.ErrModelNotFound) {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(row, "colour", "red") })); !errors.Is(err, efl.ErrModelNotFound) {
t.Errorf("setting a property a row lacks gave %v, want ErrModelNotFound", err)
}
efl.Sync(func() {
diff --git a/efl/eo/loop_consumer.go b/efl/eo/loop_consumer.go
index baa532a2..899e27ef 100644
--- a/efl/eo/loop_consumer.go
+++ b/efl/eo/loop_consumer.go
@@ -94,6 +94,6 @@ func (o *LoopConsumer) FutureResolved(result *efl.Value) *efl.Future[*efl.Value]
// be resolved and dispatched by the loop scheduler as usual.
//
// This is a helper that behaves the same as @ref eina_future_rejected.
-func (o *LoopConsumer) FutureRejected(error int) *efl.Future[error] {
+func (o *LoopConsumer) FutureRejected(error int) *efl.Future[struct{}] {
return cEflLoopFutureRejected(eort.Checked(eort.Eo(o)), error)
}
diff --git a/efl/eo/model_test.go b/efl/eo/model_test.go
index 769eb69f..31ab9fd8 100644
--- a/efl/eo/model_test.go
+++ b/efl/eo/model_test.go
@@ -1,9 +1,11 @@
package eo_test
import (
+ "context"
"errors"
"slices"
"testing"
+ "time"
"git.enlightenment.org/cedric/ego/efl"
"git.enlightenment.org/cedric/ego/efl/eo"
@@ -56,14 +58,14 @@ func TestModelChildAddedPayload(t *testing.T) {
// build or free.
func TestReadWriteProperty(t *testing.T) {
var m *eo.GenericModel
- var wrote <-chan error
+ var wrote *efl.Future[*efl.Value]
efl.Sync(func() {
m, _ = eo.NewGenericModel(efl.MainLoop())
wrote = eo.WriteProperty(m, "age", 36)
})
defer efl.Sync(func() { m.Del() })
- if err := <-wrote; err != nil {
+ if err := written(t, wrote); err != nil {
t.Fatalf("WriteProperty: %v", err)
}
var age int
@@ -79,3 +81,12 @@ func TestReadWriteProperty(t *testing.T) {
t.Fatalf("a missing property reads as %v, want ErrModelNotFound", missingErr)
}
}
+
+// written waits for the future of a property write and returns its error.
+func written(t *testing.T, f *efl.Future[*efl.Value]) error {
+ t.Helper()
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ _, err := f.Await(ctx)
+ return err
+}
diff --git a/efl/eo/property.go b/efl/eo/property.go
index 30a07a22..856ba66f 100644
--- a/efl/eo/property.go
+++ b/efl/eo/property.go
@@ -1,10 +1,6 @@
package eo
-import (
- "context"
-
- "git.enlightenment.org/cedric/ego/efl"
-)
+import "git.enlightenment.org/cedric/ego/efl"
// ReadProperty reads the property name of m as a T: any type
// efl.ValueUnmarshal fills, a scalar, a slice, a map or a struct.
@@ -28,30 +24,21 @@ func ReadProperty[T any](m Model, name string) (T, error) {
}
// WriteProperty sets the property name of m to value, converted the way
-// efl.ValueMarshal converts it.
+// efl.ValueMarshal converts it, and returns the future the model answers
+// with, which is what m.SetProperty returns.
//
// A model may have to reach storage, a bus or a thread before it accepts a
-// value, so the outcome arrives on the channel: nil once the value is
-// accepted, the model's error if it is refused. The channel is closed after
-// that one result. Must be called on the EFL thread; the channel can be read
-// from anywhere, but not from the EFL thread before the loop has run again.
-func WriteProperty(m Model, name string, value any) <-chan error {
- out := make(chan error, 1)
-
+// value, so the outcome arrives on the future: it resolves once the value is
+// accepted, and is rejected with the model's error if it is refused. A value
+// that cannot be marshalled is rejected at once with the marshalling error.
+// Must be called on the EFL thread; the future is awaited from a goroutine,
+// as any future is.
+func WriteProperty(m Model, name string, value any) *efl.Future[*efl.Value] {
v, err := efl.ValueMarshal(value)
if err != nil {
- out <- err
- close(out)
- return out
+ return efl.Rejected[*efl.Value](err)
}
// The model copies what it keeps.
- done := m.SetProperty(name, v)
- v.Free()
-
- go func() {
- defer close(out)
- _, err := done.Await(context.Background())
- out <- err
- }()
- return out
+ defer v.Free()
+ return m.SetProperty(name, v)
}
diff --git a/efl/eo/struct_model_children_test.go b/efl/eo/struct_model_children_test.go
index 081c64bd..165d7bb3 100644
--- a/efl/eo/struct_model_children_test.go
+++ b/efl/eo/struct_model_children_test.go
@@ -126,10 +126,10 @@ func TestStructModelRowsRefuseWrites(t *testing.T) {
m, view := newTown(t, town{Hours: threeHours()})
row := rows(t, view, 0, 1)[0]
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(row, "label", "noon") }); !errors.Is(err, efl.ErrModelReadOnly) {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(row, "label", "noon") })); !errors.Is(err, efl.ErrModelReadOnly) {
t.Errorf("writing a row gave %v, want ErrModelReadOnly", err)
}
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(row, "colour", "red") }); !errors.Is(err, efl.ErrModelNotFound) {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(row, "colour", "red") })); !errors.Is(err, efl.ErrModelNotFound) {
t.Errorf("writing an unknown row property gave %v, want ErrModelNotFound", err)
}
efl.Sync(func() {
@@ -251,7 +251,7 @@ func TestStructModelWriteLeavesRowsAlone(t *testing.T) {
view.OnChildRemoved(func(efl.ModelChildrenEvent) { events++ })
})
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(view, "place", "Lyon") }); err != nil {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(view, "place", "Lyon") })); err != nil {
t.Fatalf("writing place: %v", err)
}
efl.Sync(func() {
diff --git a/efl/eo/struct_model_test.go b/efl/eo/struct_model_test.go
index 45ae9161..49289179 100644
--- a/efl/eo/struct_model_test.go
+++ b/efl/eo/struct_model_test.go
@@ -84,7 +84,7 @@ func TestStructModelSetProperty(t *testing.T) {
view.OnPropertiesChanged(func(ev efl.ModelPropertyEvent) { changed = append(changed, ev.ChangedProperties) })
})
write := func(name string, value any) error {
- return <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(view, name, value) })
+ return written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(view, name, value) }))
}
if err := write("weight", 200); err != nil {
@@ -200,10 +200,10 @@ func TestStructModelNamedFields(t *testing.T) {
}
defer efl.Sync(func() { m.Del() })
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(view, "kind", "b") }); err != nil {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(view, "kind", "b") })); err != nil {
t.Errorf("writing a named string: %v", err)
}
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(view, "kind", 3) }); !errors.Is(err, efl.ErrModelIncorrectValue) {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(view, "kind", 3) })); !errors.Is(err, efl.ErrModelIncorrectValue) {
t.Errorf("writing an int into a named string gave %v", err)
}
efl.Sync(func() {
@@ -438,7 +438,7 @@ func TestStructModelWriteAllocatesThePointee(t *testing.T) {
view.OnPropertiesChanged(func(ev efl.ModelPropertyEvent) { changed = append(changed, ev.ChangedProperties) })
})
write := func(name string, value any) error {
- return <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(view, name, value) })
+ return written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(view, name, value) }))
}
if err := write("now_celsius", 12.5); err != nil {
@@ -468,7 +468,7 @@ func TestStructModelWriteAllocatesThePointee(t *testing.T) {
// A view can also fill a pointer that was nil.
_, fresh := newStructForecast(t, forecast{})
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(fresh, "now_celsius", 3.0) }); err != nil {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(fresh, "now_celsius", 3.0) })); err != nil {
t.Errorf("writing into a nil pointer field: %v", err)
}
efl.Sync(func() {
@@ -578,7 +578,7 @@ func TestStructModelPointersToStructsAndNamedTypes(t *testing.T) {
t.Cleanup(func() { efl.Sync(func() { m.Del() }) })
write := func(name string, value any) error {
- return <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(view, name, value) })
+ return written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(view, name, value) }))
}
efl.Sync(func() { m.Set(reading{Spot: &spot{X: 1, Y: 2}}) })
if err := write("feeling", 3.5); err != nil {
@@ -626,7 +626,7 @@ func TestStructModelPointerInEmbeddedStruct(t *testing.T) {
t.Errorf("a nil embedded pointer read as %v, want EAGAIN", err)
}
})
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(view, "wind", 12.0) }); err != nil {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(view, "wind", 12.0) })); err != nil {
t.Errorf("writing the embedded pointer: %v", err)
}
efl.Sync(func() {
diff --git a/efl/eort/future.go b/efl/eort/future.go
index 9d01bb0b..4c67493f 100644
--- a/efl/eort/future.go
+++ b/efl/eort/future.go
@@ -184,19 +184,6 @@ func ScalarToValue[T efl.ScalarType](v T) (*efl.Value, error) {
return efl.ValueMarshal(v)
}
-// ErrorFromValue is for a future of an Eina_Error. A failure rejects the
-// future, so what is left to resolve with is no error.
-func ErrorFromValue(v *efl.Value) (error, error) {
- if p := valueOf(v); p != nil && v.Kind() != efl.ValueKindError {
- return nil, unexpected("an error", p)
- }
- return nil, nil
-}
-
-// ErrorToValue resolves a future of an Eina_Error: a non-nil one rejects it,
-// which is how EFL carries it.
-func ErrorToValue(result error) (*efl.Value, error) { return nil, result }
-
// ContentFromValue is for a future of content, nil for one that resolved with
// nothing.
func ContentFromValue(v *efl.Value) (*efl.Content, error) {
diff --git a/efl/error.go b/efl/error.go
index 3c03e599..4accb0db 100644
--- a/efl/error.go
+++ b/efl/error.go
@@ -15,6 +15,7 @@ import (
"context"
"errors"
"fmt"
+ "os"
"syscall"
)
@@ -49,7 +50,9 @@ func (e *EinaError) Unwrap() error {
// Is reports whether e is target: the same code, whether target is another
// EinaError or one of the sentinels such as ErrModelNotFound. A cancelled
-// future fails with ECANCELED, which is context.Canceled too.
+// future fails with ECANCELED, which is context.Canceled too, and one that
+// timed out with ETIMEDOUT, which is context.DeadlineExceeded and
+// os.ErrDeadlineExceeded too. syscall.Errno does not say so itself.
func (e *EinaError) Is(target error) bool {
switch t := target.(type) {
case *EinaError:
@@ -57,7 +60,13 @@ func (e *EinaError) Is(target error) bool {
case *registeredError:
return t.code() == e.Code
}
- return target == context.Canceled && e.Code == int(syscall.ECANCELED)
+ switch e.Code {
+ case int(syscall.ECANCELED):
+ return target == context.Canceled
+ case int(syscall.ETIMEDOUT):
+ return target == context.DeadlineExceeded || target == os.ErrDeadlineExceeded
+ }
+ return false
}
// registeredError is an error EFL registers when it starts, so its code is
@@ -103,7 +112,8 @@ var (
)
// errorCode is the Eina_Error EFL is told err is: its own code for an EFL
-// error or an errno, EINVAL for anything else.
+// error or an errno, ETIMEDOUT for context.DeadlineExceeded, EINVAL for
+// anything else.
func errorCode(err error) int {
var e *EinaError
var r *registeredError
@@ -117,6 +127,8 @@ func errorCode(err error) int {
return r.code()
case errors.As(err, &errno):
return int(errno)
+ case errors.Is(err, context.DeadlineExceeded):
+ return int(syscall.ETIMEDOUT)
}
return int(syscall.EINVAL)
}
diff --git a/efl/future.go b/efl/future.go
index ebd128d4..ee9eff59 100644
--- a/efl/future.go
+++ b/efl/future.go
@@ -39,8 +39,8 @@ import "C"
import (
"context"
+ "errors"
"sync"
- "syscall"
"unsafe"
)
@@ -80,12 +80,17 @@ type futureState struct {
// non-nil err rejects the future, and the value is then ignored; a *Value
// carrying an error rejects it with that error.
//
+// A value settles the future with becomes the future's: every Await returns
+// that same value, and the one EFL is given is a copy made later, on the loop
+// thread. The producer must neither free nor change a *Value, or an object
+// held in a value, after passing it to settle, from any goroutine; the
+// garbage collector releases it once nobody holds it. Objects in the value
+// must live until EFL has its copy, which a wrapper owning a reference, as
+// Object.Ref returns, sees to.
+//
// A future an implementation hands EFL is settled with context.Canceled when
// EFL cancels it, so a producer that Awaits it finds out and can stop its
-// work; give each call a future of its own. Objects in the value are given to
-// EFL on its thread, when settle is called there and otherwise once the loop
-// gets to it: they must live until then, which a wrapper owning a reference,
-// as Object.Ref returns, sees to.
+// work; give each call a future of its own.
func NewFuture[T any]() (f *Future[T], settle func(T, error)) {
f = &Future[T]{state: futureState{done: make(chan struct{})}}
return f, func(value T, err error) {
@@ -100,20 +105,27 @@ func NewFuture[T any]() (f *Future[T], settle func(T, error)) {
}
}
-// Resolved returns a future that has already succeeded with value.
+// Resolved returns a future that has already succeeded with value, which
+// belongs to the future from now on, as NewFuture says of what settle is
+// given.
func Resolved[T any](value T) *Future[T] {
f, settle := NewFuture[T]()
settle(value, nil)
return f
}
+// errRejectedNil is what a future rejected with no error is rejected with.
+var errRejectedNil = errors.New("efl: Rejected called with a nil error")
+
// Rejected returns a future that has already failed with err. EFL is told of
// it as its Eina_Error code -- a sentinel such as ErrModelReadOnly, an
-// *EinaError, a syscall.Errno, or ECANCELED for context.Canceled -- and any
-// other error, nil included, as EINVAL.
+// *EinaError, a syscall.Errno, ECANCELED for context.Canceled, or ETIMEDOUT
+// for context.DeadlineExceeded -- and any other error as EINVAL. A nil err
+// is a mistake of the caller's: Await returns an error saying so, and EFL is
+// told EINVAL.
func Rejected[T any](err error) *Future[T] {
if err == nil {
- err = syscall.EINVAL
+ err = errRejectedNil
}
f, settle := NewFuture[T]()
var none T
@@ -127,8 +139,14 @@ func Rejected[T any](err error) *Future[T] {
//
// When ctx ends first, ctx's error is returned, and the EFL future behind f,
// if there is one, is cancelled on the EFL thread -- whatever it was waiting
-// for is let go rather than left running for nobody, and every other Await of
-// f then gets context.Canceled.
+// for is let go rather than left running for nobody. That is the future's
+// end for every goroutine waiting for it, not only the one whose context
+// ended: their Awaits return context.Canceled. A future made purely in Go,
+// with no EFL future behind it, is not cancelled by a context: Await returns
+// the context's error and the future is as it was.
+//
+// Every Await returns the same value: a *Value is one value for all of them,
+// so freeing it frees it for each.
//
// A nil Future is a cancelled one. Await may be called by several goroutines,
// and more than once.
@@ -204,15 +222,16 @@ func (s *futureState) whenSettled(fn func()) {
}
// cancelSource cancels the EFL future behind s, if there is one that has not
-// settled yet. From another goroutine it posts to the EFL thread and returns
-// without waiting for it.
+// settled yet, on the EFL thread. It never waits for the loop: a caller whose
+// context has ended must not be held by a loop that has no room for the
+// request.
func (s *futureState) cancelSource() {
+ // Written by watch before the future is published, and never after, so
+ // it is read without the lock.
if !s.fromEFL {
return
}
- // Waits for room rather than leave a future that nobody waits for
- // running; fails only once EFL is shutting down.
- PostContext(context.Background(), func() {
+ postWithoutWaiting(func() {
if f := s.source; f != nil {
s.source = nil
C.eina_future_cancel(f)
diff --git a/efl/future_test.go b/efl/future_test.go
index 2fc48344..e995b7fe 100644
--- a/efl/future_test.go
+++ b/efl/future_test.go
@@ -3,8 +3,10 @@ package efl_test
import (
"context"
"errors"
+ "fmt"
"image"
"io/fs"
+ "os"
"reflect"
"runtime"
"slices"
@@ -68,7 +70,6 @@ func TestFutureCarriesItsType(t *testing.T) {
expect(t, 2.5, eort.ScalarToValue[float64], eort.ScalarFromValue[float64])
expect(t, weekday(3), eort.ScalarToValue[weekday], eort.ScalarFromValue[weekday])
expect(t, image.Rect(1, 2, 4, 6), eort.RectToValue, eort.RectFromValue)
- expect[error](t, nil, eort.ErrorToValue, eort.ErrorFromValue)
value, err := roundTrip(t, efl.NewValueSlice([]int{1, 2, 3}), eort.AnyToValue, eort.AnyFromValue)
if err != nil {
@@ -215,6 +216,26 @@ func TestFutureObjectsOwnAReference(t *testing.T) {
}
}
+// A nil in a slice of objects stays where it is, so that the others keep their
+// indices.
+func TestFutureObjectsKeepNil(t *testing.T) {
+ children := childModels(t, 2)
+ got, err := roundTrip(t, []*efl.Object{children[0], nil, children[1]}, eort.ObjectsToValue, eort.ObjectsFromValue)
+ if err != nil || len(got) != 3 {
+ t.Fatalf("three objects with a nil came back as %v, %v", got, err)
+ }
+ efl.Sync(func() {
+ if got[1] != nil {
+ t.Errorf("the nil came back as %v", got[1])
+ }
+ if !efl.Same(got[0], children[0]) || !efl.Same(got[2], children[1]) {
+ t.Error("the objects around the nil moved")
+ }
+ got[0].Unref()
+ got[2].Unref()
+ })
+}
+
// What an implementation resolves with on the loop is taken there and then:
// it may delete its objects before the loop turns again.
func TestFutureTakesObjectsAsTheyAreHandedOver(t *testing.T) {
@@ -257,6 +278,10 @@ func TestFutureRejection(t *testing.T) {
{"a cancellation", context.Canceled, []error{context.Canceled, syscall.ECANCELED}},
{"any other", errors.New("no code for this"), []error{syscall.EINVAL}},
{"no error at all", nil, []error{syscall.EINVAL}},
+ {"a deadline", context.DeadlineExceeded,
+ []error{context.DeadlineExceeded, os.ErrDeadlineExceeded, syscall.ETIMEDOUT}},
+ {"a deadline wrapped", fmt.Errorf("fetching: %w", context.DeadlineExceeded),
+ []error{context.DeadlineExceeded, syscall.ETIMEDOUT}},
} {
got := onLoop(func() *efl.Future[string] {
return throughEFL(efl.Rejected[string](c.reject), eort.ScalarToValue[string], eort.ScalarFromValue[string])
@@ -274,12 +299,24 @@ func TestFutureRejection(t *testing.T) {
}
}
+ // Awaited from Go, a rejection with no error says that is what it was.
+ if _, err := efl.Rejected[string](nil).Await(soon(t)); err == nil || !strings.Contains(err.Error(), "nil error") {
+ t.Errorf("Rejected(nil) gave %v, want an error saying it was given none", err)
+ }
+ var eina *efl.EinaError
+ if _, err := efl.Rejected[string](context.DeadlineExceeded).Await(soon(t)); !errors.Is(err, context.DeadlineExceeded) || errors.As(err, &eina) {
+ t.Errorf("a deadline rejected in Go gave %v, want it as it was", err)
+ }
+
// A value carrying an error is a rejection too, with no EFL between.
value, err := efl.Resolved(efl.NewErrorValue(efl.ErrModelNotFound)).Await(soon(t))
if value != nil || !errors.Is(err, efl.ErrModelNotFound) {
t.Errorf("an error value gave %v, %v", value, err)
}
- if _, err := roundTrip[error](t, efl.ErrModelNotFound, eort.ErrorToValue, eort.ErrorFromValue); !errors.Is(err, efl.ErrModelNotFound) {
+ rejected := onLoop(func() *efl.Future[struct{}] {
+ return throughEFL(efl.Rejected[struct{}](efl.ErrModelNotFound), eort.VoidToValue, eort.VoidFromValue)
+ })
+ if _, err := rejected.Await(soon(t)); !errors.Is(err, efl.ErrModelNotFound) {
t.Errorf("a future of an error gave %v", err)
}
if _, err := roundTrip(t, nil, eort.AnyToValue, eort.AnyFromValue); !errors.Is(err, context.Canceled) {
@@ -307,7 +344,6 @@ func TestFutureOfAnotherType(t *testing.T) {
"a bool for a string": failureOf(t, text, toText, eort.ScalarFromValue[bool]),
"content for a string": failureOf(t, text, toText, eort.ContentFromValue),
"a rectangle for a string": failureOf(t, text, toText, eort.RectFromValue),
- "an error for a string": failureOf(t, text, toText, eort.ErrorFromValue),
"a string for nothing": failureOf(t, struct{}{}, eort.VoidToValue, eort.ScalarFromValue[string]),
} {
var eina *efl.EinaError
@@ -350,6 +386,44 @@ func TestFutureCancellationReachesTheProducer(t *testing.T) {
}
}
+// A caller whose context ends is not held by a loop with no room for the
+// cancellation: Await returns at the deadline, and the future is cancelled
+// once the loop runs again.
+func TestFutureAwaitReturnsAtItsDeadlineWithAFullQueue(t *testing.T) {
+ var produced *efl.Future[int]
+ consumed := onLoop(func() *efl.Future[int] {
+ produced, _ = efl.NewFuture[int]()
+ return throughEFL(produced, eort.ScalarToValue[int], eort.ScalarFromValue[int])
+ })
+
+ release := blockLoop(t)
+ for range 300 {
+ go efl.PostContext(context.Background(), func() {})
+ }
+ time.Sleep(50 * time.Millisecond)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
+ defer cancel()
+ returned := make(chan error, 1)
+ go func() {
+ _, err := consumed.Await(ctx)
+ returned <- err
+ }()
+ select {
+ case err := <-returned:
+ if !errors.Is(err, context.DeadlineExceeded) {
+ t.Errorf("Await = %v, want the deadline", err)
+ }
+ case <-time.After(2 * time.Second):
+ t.Error("Await outlived its context while the loop's queue was full")
+ }
+
+ release()
+ if _, err := produced.Await(soon(t)); !errors.Is(err, context.Canceled) {
+ t.Errorf("the producer's future gave %v once the loop ran, want it cancelled", err)
+ }
+}
+
// One of EFL's futures that an implementation hands on is cancelled with it.
func TestFutureCancellationReachesAForwardedFuture(t *testing.T) {
var timeout *efl.Future[struct{}]
@@ -392,6 +466,36 @@ func TestFutureSettledLater(t *testing.T) {
}
}
+// A producer settles with a value from a goroutine and does not touch it
+// again: EFL is given a copy on the loop, and every Await returns the value
+// itself.
+func TestFutureSettledWithAValueFromAGoroutine(t *testing.T) {
+ var produced *efl.Future[*efl.Value]
+ var settle func(*efl.Value, error)
+ consumed := onLoop(func() *efl.Future[*efl.Value] {
+ produced, settle = efl.NewFuture[*efl.Value]()
+ return throughEFL(produced, eort.AnyToValue, eort.AnyFromValue)
+ })
+ sent := efl.NewValueSlice([]int{1, 2, 3})
+ var producer sync.WaitGroup
+ producer.Go(func() { settle(sent, nil) })
+ producer.Wait()
+
+ got, err := consumed.Await(soon(t))
+ if err != nil {
+ t.Fatal(err)
+ }
+ efl.Sync(func() {
+ if ints, err := efl.ValueSlice[int](got); err != nil || !slices.Equal(ints, []int{1, 2, 3}) {
+ t.Errorf("EFL's copy is %v, %v, want 1 2 3", ints, err)
+ }
+ got.Free()
+ })
+ if same, err := produced.Await(soon(t)); err != nil || same != sent {
+ t.Errorf("the producer's future gave %p, %v, want the value it was settled with", same, err)
+ }
+}
+
// Every Await of a future gets the same result, however many there are.
func TestFutureAwaitedMoreThanOnce(t *testing.T) {
f, settle := efl.NewFuture[*efl.Value]()
diff --git a/efl/ui/view_model_derive_test.go b/efl/ui/view_model_derive_test.go
index e46878d6..b218036d 100644
--- a/efl/ui/view_model_derive_test.go
+++ b/efl/ui/view_model_derive_test.go
@@ -116,8 +116,8 @@ func TestDerivePropertyIsReadOnly(t *testing.T) {
t.Error(err)
}
})
- errc := efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(rows[0], "one", 2) })
- if err := <-errc; !errors.Is(err, efl.ErrModelReadOnly) {
+ errc := efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(rows[0], "one", 2) })
+ if err := written(t, errc); !errors.Is(err, efl.ErrModelReadOnly) {
t.Errorf("writing a derived property gave %v, want ErrModelReadOnly", err)
}
}
@@ -160,7 +160,7 @@ func TestDerivePropertyChangesWithItsSources(t *testing.T) {
changed = append(changed, ev.ChangedProperties)
})
})
- if err := <-efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(rows[0], "n", 5) }); err != nil {
+ if err := written(t, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(rows[0], "n", 5) })); err != nil {
t.Fatal(err)
}
efl.Sync(func() {
diff --git a/efl/ui/view_model_logic_test.go b/efl/ui/view_model_logic_test.go
index 0a9aabd5..cf5d2027 100644
--- a/efl/ui/view_model_logic_test.go
+++ b/efl/ui/view_model_logic_test.go
@@ -3,6 +3,7 @@ package ui_test
import (
"context"
"errors"
+ "runtime"
"testing"
"time"
@@ -83,8 +84,8 @@ func TestViewModelLogicFromGo(t *testing.T) {
}
})
- errc := efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(kids[0], "double", 10) })
- if err := <-errc; err != nil {
+ errc := efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(kids[0], "double", 10) })
+ if err := written(t, errc); err != nil {
t.Fatalf("writing double: %v", err)
}
efl.Sync(func() {
@@ -93,8 +94,66 @@ func TestViewModelLogicFromGo(t *testing.T) {
}
})
- errc = efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(kids[0], "triple", 9) })
- if err := <-errc; !errors.Is(err, efl.ErrModelReadOnly) {
+ errc = efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(kids[0], "triple", 9) })
+ if err := written(t, errc); !errors.Is(err, efl.ErrModelReadOnly) {
t.Errorf("writing a property with no set function gave %v, want ErrModelReadOnly", err)
}
}
+
+// A write to a model that never answers holds no goroutine: the future is the
+// model's own, and a value that cannot be marshalled is refused by it.
+func TestWritePropertyReturnsTheModelsFuture(t *testing.T) {
+ var vm *ui.ViewModel
+ var src *eo.GenericModel
+ efl.Sync(func() {
+ var err error
+ if src, err = eo.NewGenericModel(efl.MainLoop()); err != nil {
+ t.Error(err)
+ return
+ }
+ if vm, err = ui.NewViewModel(efl.MainLoop(), viewmodel.WithModel(src)); err != nil {
+ t.Error(err)
+ return
+ }
+ get := func(*efl.Object, string) *efl.Value { v, _ := efl.NewValue(1); return v }
+ set := func(*efl.Object, string, *efl.Value) *efl.Future[*efl.Value] {
+ stuck, _ := efl.NewFuture[*efl.Value]()
+ return stuck
+ }
+ if err := vm.PropertyLogicAdd("stuck", nil, get, set); err != nil {
+ t.Error(err)
+ }
+ })
+ if vm == nil {
+ return
+ }
+ defer efl.Sync(func() { vm.Del(); src.Del() })
+
+ before := runtime.NumGoroutine()
+ var writes []*efl.Future[*efl.Value]
+ for range 20 {
+ writes = append(writes, efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(vm, "stuck", 1) }))
+ }
+ if grew := runtime.NumGoroutine() - before; grew >= 10 {
+ t.Errorf("20 writes that never settle started %d goroutines", grew)
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
+ defer cancel()
+ if _, err := writes[0].Await(ctx); !errors.Is(err, context.DeadlineExceeded) {
+ t.Errorf("a write that never settles gave %v, want the deadline", err)
+ }
+
+ refused := efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(vm, "stuck", make(chan int)) })
+ if err := written(t, refused); err == nil {
+ t.Error("a value that cannot be marshalled was accepted")
+ }
+}
+
+// written waits for the future of a property write and returns its error.
+func written(t *testing.T, f *efl.Future[*efl.Value]) error {
+ t.Helper()
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ _, err := f.Await(ctx)
+ return err
+}
diff --git a/efl/value.go b/efl/value.go
index 3a399175..f768b740 100644
--- a/efl/value.go
+++ b/efl/value.go
@@ -456,9 +456,10 @@ func valueForC(v *Value) *C.Eina_Value {
// NewErrorValue makes an error value carrying err, the shape EFL gives a
// failure in: what a model's Property returns for a property it cannot
-// give. err is carried as its Eina_Error code -- a sentinel such as ErrModelReadOnly, an
-// *EinaError, or a syscall.Errno -- and any other error as EINVAL. The value
-// is owned, as NewValue's is.
+// give. err is carried as its Eina_Error code -- a sentinel such as
+// ErrModelReadOnly, an *EinaError, a syscall.Errno, or ETIMEDOUT for
+// context.DeadlineExceeded -- and any other error as EINVAL. The value is
+// owned, as NewValue's is.
func NewErrorValue(err error) *Value {
return armed(&Value{ptr: C._ego_value_error_new(C.int(errorCode(err))), owned: true})
}
@@ -474,7 +475,8 @@ func NewErrorValue(err error) *Value {
//
// Each object is referenced, so the returned wrappers stay valid after the
// value they came out of is freed. They own that reference: Unref them, or
-// leave them to the garbage collector.
+// leave them to the garbage collector. An element holding no object is nil,
+// so that the others keep their indices.
//
// Must be called on the EFL thread.
func (v *Value) Objects() []*Object {
@@ -493,11 +495,7 @@ func (v *Value) Objects() []*Object {
out := make([]*Object, 0, n)
for i := 0; i < n; i++ {
- ptr := unsafe.Pointer(C._ego_value_array_object_get(cv, C.uint(i)))
- if ptr == nil {
- continue
- }
- out = append(out, wrapObject(ptr))
+ out = append(out, wrapObject(unsafe.Pointer(C._ego_value_array_object_get(cv, C.uint(i)))))
}
return out
}
diff --git a/examples/weather/main_test.go b/examples/weather/main_test.go
index 91f3de6c..1918b111 100644
--- a/examples/weather/main_test.go
+++ b/examples/weather/main_test.go
@@ -620,8 +620,8 @@ func TestUnitUpdatesRowsAndNow(t *testing.T) {
})
harness.WaitFor(t, a.win, patience, "Paris to be shown", func() bool { return a.now.Text() != "" })
- errc := efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(a.shown.view, "unit", int(fahrenheit)) })
- if err := <-errc; err != nil {
+ errc := efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(a.shown.view, "unit", int(fahrenheit)) })
+ if err := written(t, errc); err != nil {
t.Fatal(err)
}
showsHours(t, a, items, "the rows in Fahrenheit", []string{
@@ -644,8 +644,8 @@ func TestUnitRefusesWhatIsNotAUnit(t *testing.T) {
s := newService(t)
a := open(t, s)
- errc := efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(a.shown.view, "unit", 7) })
- if err := <-errc; !errors.Is(err, efl.ErrModelIncorrectValue) {
+ errc := efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(a.shown.view, "unit", 7) })
+ if err := written(t, errc); !errors.Is(err, efl.ErrModelIncorrectValue) {
t.Errorf("writing 7 as the unit gave %v, want ErrModelIncorrectValue", err)
}
efl.Sync(func() {
@@ -663,8 +663,8 @@ func TestUnitStaysWhenTheCityChanges(t *testing.T) {
efl.Sync(func() { a.find("Paris", true) })
harness.WaitFor(t, a.win, patience, "Paris to be shown", func() bool { return a.now.Text() != "" })
- errc := efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(a.shown.view, "unit", int(fahrenheit)) })
- if err := <-errc; err != nil {
+ errc := efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(a.shown.view, "unit", int(fahrenheit)) })
+ if err := written(t, errc); err != nil {
t.Fatal(err)
}
@@ -766,8 +766,8 @@ func TestDetailFollowsTheSelection(t *testing.T) {
harness.WaitFor(t, a.win, patience, "the other hour's details", func() bool { return a.detail.Text() == "At 16:00: clear, 11.5°C" })
// The row says its temperature changed, and the detail follows.
- errc := efl.SyncWithReturn(func() <-chan error { return eo.WriteProperty(a.shown.view, "unit", int(fahrenheit)) })
- if err := <-errc; err != nil {
+ errc := efl.SyncWithReturn(func() *efl.Future[*efl.Value] { return eo.WriteProperty(a.shown.view, "unit", int(fahrenheit)) })
+ if err := written(t, errc); err != nil {
t.Fatal(err)
}
harness.WaitFor(t, a.win, patience, "the detail in Fahrenheit", func() bool { return a.detail.Text() == "At 16:00: clear, 52.7°F" })
@@ -1629,3 +1629,12 @@ func TestEscapeWithTheDropdownOpenClosesItAndAbandonsTheLookup(t *testing.T) {
}
})
}
+
+// written waits for the future of a property write and returns its error.
+func written(t *testing.T, f *efl.Future[*efl.Value]) error {
+ t.Helper()
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ _, err := f.Await(ctx)
+ return err
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.