Skip to content

Commit

Permalink
a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
wcharczuk committed Feb 12, 2024
1 parent 975ba20 commit ba8a20a
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package incr
import (
"context"
"fmt"
"sync"
"testing"

"github.com/wcharczuk/go-incr/testutil"
Expand Down Expand Up @@ -796,20 +797,34 @@ func Test_Bind_regression_parallel(t *testing.T) {
}

func makeRegressionGraph(ctx context.Context) (*Graph, ObserveIncr[*int]) {
cacheMu := sync.Mutex{}
cache := make(map[string]Incr[*int])

getCached := func(key string) (out Incr[*int], ok bool) {
cacheMu.Lock()
out, ok = cache[key]
cacheMu.Unlock()
return
}
setCached := func(key string, i Incr[*int]) {
cacheMu.Lock()
cache[key] = i
cacheMu.Unlock()
}

graph := New()

fakeFormula := Var(graph, "fakeformula")
fakeFormula.Node().SetLabel("fakeformula")
var f func(Scope, int) Incr[*int]
f = func(bs Scope, t int) Incr[*int] {
key := fmt.Sprintf("f-%d", t)
if cached, ok := cache[key]; ok {
if cached, ok := getCached(key); ok {
return WithinScope(bs, cached)
}
r := Bind(bs, fakeFormula, func(bs Scope, formula string) Incr[*int] {
key := fmt.Sprintf("map-f-%d", t)
if cached, ok := cache[key]; ok {
if cached, ok := getCached(key); ok {
return WithinScope(bs, cached)
}
if t == 0 {
Expand All @@ -823,25 +838,25 @@ func makeRegressionGraph(ctx context.Context) (*Graph, ObserveIncr[*int]) {
return &out
})
bindOutput.Node().SetLabel(fmt.Sprintf("map-f-%d", t))
cache[key] = bindOutput
setCached(key, bindOutput)
return bindOutput
})
r.Node().SetLabel(key)
cache[key] = r
setCached(key, r)
return r
}

g := func(bs Scope, t int) Incr[*int] {
key := fmt.Sprintf("g-%d", t)
if cached, ok := cache[key]; ok {
if cached, ok := getCached(key); ok {
return WithinScope(bs, cached)
}
r := Bind(bs, fakeFormula, func(bs Scope, formula string) Incr[*int] {
output := f(bs, t)
return output
})
r.Node().SetLabel(key)
cache[key] = r
setCached(key, r)
return r
}

Expand Down

0 comments on commit ba8a20a

Please sign in to comment.