File: test_state.rb

package info (click to toggle)
sonic-pi 3.2.2~repack-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 71,872 kB
  • sloc: ruby: 30,548; cpp: 8,490; sh: 957; ansic: 461; erlang: 360; lisp: 141; makefile: 44
file content (210 lines) | stat: -rw-r--r-- 4,903 bytes parent folder | download | duplicates (4)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#--
# This file is part of Sonic Pi: http://sonic-pi.net
# Full project source: https://github.com/samaaron/sonic-pi
# License: https://github.com/samaaron/sonic-pi/blob/master/LICENSE.md
#
# Copyright 2017 by Sam Aaron (http://sam.aaron.name).
# All rights reserved.
#
# Permission is granted for use, copying, modification, and
# distribution of modified versions of this work as long as this
# notice is included.
#++

require_relative "../../setup_test"


module SonicPi

  class StateTester < Minitest::Test
    def setup
      @lang = SonicPi::MockLang.new
    end

    def test_get_with_no_set
      @lang.instance_eval do
        assert_equal nil, get(:foodbarbaz)
      end
    end

    def test_get_defaults
      @lang.run do
        assert_equal :bar, get(:foo, :bar)
      end
    end

    def test_basic_set
      @lang.run do
        set(:foo, 1)
        get(:foo)
        assert_equal 1, get(:foo, 999)
        set(:foo, 300)
        get(:foo)
        assert_equal 300, get(:foo, 9998)
      end
    end

    def test_time_warp_future_setting
      @lang.run do
        assert_equal nil, get(:intensity)
        10.times do
          # TODO - remove these promises
          # when thread waiting is implemented
          p1 = Promise.new
          p2 = Promise.new

          in_thread do
            # TODO: remove this sleep when
            # thread waiting has been implemented
            # for sync and get
            sleep 0.01
            p1.get
            assert_equal get(:intensity), 100
            sleep 0.04
            p2.get
            assert_equal get(:intensity), 102
          end

          in_thread do
            set(:intensity, 100)
            p1.deliver! true
            sleep 0.03
            set(:intensity, 102)
            p2.deliver! true
          end
          sleep 0.001
        end
      end
    end


    def test_time_warp_future_setting_further
      @lang.run do
        assert_equal nil, get(:intensity)

        1.times do
          sleep 0.1
          time_warp [0.005, 0.01] do |v|
            set(:intensity, (ring 100, 900).tick)
          end

          sleep 0.005
          assert_equal get(:intensity), 100

          sleep 0.005
          assert_equal get(:intensity), 900
          sleep 0.005
          assert_equal get(:intensity), 900
          sleep(-0.01)
          assert_equal get(:intensity), 100
          sleep 0.005
          assert_equal get(:intensity), 900
        end
      end
    end

    def test_get_inside_future_time_warp_throws_an_error
      @lang.run do
        time_warp 0.1 do
          set(:amp, 0.7)
          assert_error SonicPi::Lang::Core::TimingError do
            get(:amp)
          end
        end
      end
    end

    def test_set_get_w_capital_letters
      @lang.run do
        set "/foo/baR", 0.7
        v = get["/foo/baR"]
        assert_equal 0.7, v
      end
    end

    def test_get_inside_past_time_warp
      @lang.run do
        sleep 0.05
        assert_equal get(:amp, :default), :default
        set :amp, 0.4
        assert_equal get(:amp, :default), 0.4
        sleep 0.05
        set :amp, 0.8
        assert_equal get(:amp), 0.8
#         time_warp(-0.1) do
#           v = get(:amp)
#           raise "yoohoo"
#           #assert_equal v, 0.5
#         end
      end
    end

    def test_multi_sets
      @lang.run do
        set(:foo, :bar)
        set(:foo, :baz)
        assert_equal :baz, get(:foo, :default)
      end
    end

    def test_sync
      @lang.run do
        p1 = Promise.new
        p2 = Promise.new

        in_thread do
          res = sync "/foo"
          p1.deliver! res
        end

        in_thread do
          res = sync "/foo"
          p2.deliver! res
        end
        sleep 0.01
        i = __current_thread_id
        time_warp 0.1 do
          @event_history.set current_time, 0, i, 0, 0, 60, "/foo", [2, 4, 6]
        end
        time_warp 0.2 do
          @event_history.set current_time, 0, i, 0, 0, 60, "/foo", [1, 2, 3]
        end

        assert_equal p1.get(1), [2, 4, 6]
        assert_equal p2.get(1), [2, 4, 6]
      end
    end


    def test_event_matcher_pruning
      @lang.run do
        assert_equal 0,  @event_history.event_matchers.matchers.size
        t = in_thread do
          sync "/foo/bar"
        end

        t2 = in_thread do
          sync "/foo/bar"
        end
        Kernel.sleep 0.1
        assert_equal 2,  @event_history.event_matchers.matchers.size
        t.kill
        Kernel.sleep 0.1
        assert_equal 1,  @event_history.event_matchers.matchers.size
        t2.kill
        Kernel.sleep 0.1
        assert_equal 0,  @event_history.event_matchers.matchers.size
      end
    end

    def test_set_makes_vals_thread_safe
      @lang.run do
        a = [1, 2]
        set :foo, a

        assert_equal [1, 2], get(:foo)
        assert get(:foo).sp_thread_safe?
      end
    end
  end
end