File: concurrent_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (92 lines) | stat: -rw-r--r-- 2,257 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require "./spec_helper"

private def method_with_named_args(chan, x = 1, y = 2)
  chan.send(x + y)
end

private def method_returning_2tuple(x, y)
  {x, y}
end

private def method_returning_3tuple(chan, x = 1, y = 2)
  {chan, x, y}
end

private def method_named(expected_named, chan)
  Fiber.current.name.should eq(expected_named)
  chan.close
end

describe "concurrent" do
  describe "spawn" do
    it "uses spawn macro" do
      chan = Channel(Int32).new

      spawn method_with_named_args(chan)
      chan.receive.should eq(3)

      spawn method_with_named_args(chan, y: 20)
      chan.receive.should eq(21)

      spawn method_with_named_args(chan, x: 10, y: 20)
      chan.receive.should eq(30)

      spawn method_with_named_args(*method_returning_3tuple(chan))
      chan.receive.should eq(3)

      spawn method_with_named_args(*method_returning_3tuple(chan, 30, 40))
      chan.receive.should eq(70)

      spawn method_with_named_args(*method_returning_2tuple(chan, 10))
      chan.receive.should eq(12)

      spawn method_with_named_args(chan, *method_returning_2tuple(10, 20))
      chan.receive.should eq(30)

      spawn method_with_named_args(*method_returning_2tuple(chan, 10), 20)
      chan.receive.should eq(30)
    end

    it "spawns named" do
      chan = Channel(Nil).new
      spawn(name: "sub") do
        Fiber.current.name.should eq("sub")
        chan.close
      end
      chan.receive?
    end

    it "spawns named with macro" do
      chan = Channel(Nil).new
      spawn method_named("foo", chan), name: "foo"
      chan.receive?
    end
  end

  it "accepts method call with receiver" do
    typeof(spawn String.new)
  end

  {% if flag?(:darwin) %}
    pending "schedules intermitting sleeps"
    # TODO: This spec fails on darwin, even with highly increased sleep times. Needs investigation.
  {% else %}
    it "schedules intermitting sleeps" do
      chan = Channel(Int32).new
      spawn do
        3.times do |i|
          sleep 40.milliseconds
          chan.send(i + 1)
        end
      end
      spawn do
        2.times do |i|
          sleep 100.milliseconds
          chan.send (i + 1) * 10
        end
      end

      Array(Int32).new(5) { chan.receive }.should eq [1, 2, 10, 3, 20]
    end
  {% end %}
end