File: thread.liq

package info (click to toggle)
liquidsoap 2.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,912 kB
  • sloc: ml: 67,867; javascript: 24,842; ansic: 273; xml: 114; sh: 96; lisp: 96; makefile: 26
file content (71 lines) | stat: -rw-r--r-- 1,098 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def f() =
  count = ref(0)

  def cb() =
    count := count() + 1
  end

  thread.when({true}, cb)
  thread.pause(1.)
  test.equal(count(), 1)

  # Testing a step with initial true:
  # True for the first 2s then false for 2s, then true
  t = time()

  def p() =
    cur_t = time()
    (t <= cur_t and cur_t < t + 2.) or t + 4. <= cur_t
  end

  count = ref(0)

  def cb() =
    count := count() + 1
  end

  thread.when(init=false, p, cb)
  thread.pause(5.)
  test.equal(count(), 1)

  # Testing a step with initial false:
  # false for the first 2s then true
  t = time()

  def p() =
    t + 2. <= time()
  end

  count = ref(0)

  def cb() =
    count := count() + 1
  end

  thread.when(p, cb)
  thread.pause(3.)
  test.equal(count(), 1)

  # Testing when triggering on each change
  # True for the first 2s then false
  t = time()

  def p() =
    cur_t = time()
    t <= cur_t and cur_t < t + 2.
  end

  count = ref(0)

  def cb() =
    count := count() + 1
  end

  thread.when(changed=false, every=0.5, p, cb)
  thread.pause(5.)
  test.equal(count(), 4)

  test.pass()
end

test.check(f)