File: xsync_go1.19_test.go

package info (click to toggle)
golang-github-bradenaw-juniper 0.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 872 kB
  • sloc: sh: 27; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 603 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//go:build go1.19

package xsync

import (
	"fmt"
	"time"
)

func ExampleWatchable() {
	start := time.Now()

	var w Watchable[int]
	w.Set(0)
	go func() {
		for i := 1; i < 20; i++ {
			w.Set(i)
			fmt.Printf("set %d at %s\n", i, time.Since(start).Round(time.Millisecond))
			time.Sleep(5 * time.Millisecond)
		}
	}()

	for {
		v, changed := w.Value()
		if v == 19 {
			return
		}

		fmt.Printf("observed %d at %s\n", v, time.Since(start).Round(time.Millisecond))

		// Sleep for longer between iterations to show that we don't slow down the setter.
		time.Sleep(17 * time.Millisecond)

		<-changed
	}
}