Skip to content

Commit

Permalink
Merge pull request #5 from dusk125/fix-callval
Browse files Browse the repository at this point in the history
  • Loading branch information
duysqubix authored Nov 9, 2023
2 parents 7a5eca9 + f0bbfdd commit 3eaab43
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/gopxl/mainthread/v2

go 1.21

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 6 additions & 2 deletions mainthread.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ func CallErr(f func() error) error {
}

// CallVal queues function f on the main thread and returns a value returned by f.
func CallVal[T any](f func() T) T {
func CallVal[T any](f func() T) (t T) {
checkRun()
respChan := <-donePool
defer returnDone(respChan)
callQueue <- func() {
respChan <- f()
}
return (<-respChan).(T)
v := <-respChan
if v != nil {
return v.(T)
}
return
}
49 changes: 49 additions & 0 deletions mainthread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package mainthread_test

import (
"errors"
"io"
"testing"

"github.com/gopxl/mainthread/v2"
"github.com/stretchr/testify/require"
)

func BenchmarkCall(b *testing.B) {
Expand Down Expand Up @@ -43,3 +45,50 @@ func BenchmarkCallVal(b *testing.B) {
}
mainthread.Run(run)
}

func TestCallVal_Resp(t *testing.T) {
run := func() {
f := func() *int {
v := 1
return &v
}
v := mainthread.CallVal(f)
require.NotNil(t, v)
require.Equal(t, 1, *v)
}
mainthread.Run(run)
}

func TestCallVal_nilResp(t *testing.T) {
run := func() {
f := func() *int {
return nil
}
v := mainthread.CallVal(f)
require.Nil(t, v)
}
mainthread.Run(run)
}

func TestCallErr_Resp(t *testing.T) {
run := func() {
f := func() error {
return io.EOF
}
v := mainthread.CallErr(f)
require.NotNil(t, v)
require.EqualError(t, v, io.EOF.Error())
}
mainthread.Run(run)
}

func TestCallErr_nilResp(t *testing.T) {
run := func() {
f := func() error {
return nil
}
v := mainthread.CallErr(f)
require.NoError(t, v)
}
mainthread.Run(run)
}

0 comments on commit 3eaab43

Please sign in to comment.