2020-03-24 18:10:56 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::Rule::Lint
|
|
|
|
describe SharedVarInFiber do
|
|
|
|
subject = SharedVarInFiber.new
|
|
|
|
|
|
|
|
it "doesn't report if there is only local shared var in fiber" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
spawn do
|
|
|
|
i = 1
|
|
|
|
puts i
|
|
|
|
end
|
|
|
|
|
|
|
|
Fiber.yield
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if there is only block shared var in fiber" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
10.times do |i|
|
|
|
|
spawn do
|
|
|
|
puts i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Fiber.yield
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if there a spawn macro is used" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
i = 0
|
|
|
|
while i < 10
|
|
|
|
spawn puts(i)
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
Fiber.yield
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is a shared var in spawn" do
|
2021-11-06 13:15:19 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
i = 0
|
|
|
|
while i < 10
|
|
|
|
spawn do
|
|
|
|
puts(i)
|
2021-10-22 17:54:39 +00:00
|
|
|
# ^ error: Shared variable `i` is used in fiber
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
Fiber.yield
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_no_corrections source
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports reassigned reference to shared var in spawn" do
|
2021-11-06 13:15:19 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
channel = Channel(String).new
|
|
|
|
n = 0
|
|
|
|
|
|
|
|
while n < 10
|
|
|
|
n = n + 1
|
|
|
|
spawn do
|
|
|
|
m = n
|
2021-10-22 17:54:39 +00:00
|
|
|
# ^ error: Shared variable `n` is used in fiber
|
2020-03-24 18:10:56 +00:00
|
|
|
channel.send m
|
|
|
|
end
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_no_corrections source
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report reassigned reference to shared var in block" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
channel = Channel(String).new
|
|
|
|
n = 0
|
|
|
|
|
|
|
|
while n < 3
|
|
|
|
n = n + 1
|
|
|
|
m = n
|
|
|
|
spawn do
|
|
|
|
channel.send m
|
|
|
|
end
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not report block is called in a spawn" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
def method(block)
|
|
|
|
spawn do
|
|
|
|
block.call(10)
|
|
|
|
end
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports multiple shared variables in spawn" do
|
2021-11-06 13:15:19 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
foo, bar, baz = 0, 0, 0
|
|
|
|
while foo < 10
|
|
|
|
baz += 1
|
|
|
|
spawn do
|
|
|
|
puts foo
|
2021-10-22 17:54:39 +00:00
|
|
|
# ^^^ error: Shared variable `foo` is used in fiber
|
2020-03-24 18:10:56 +00:00
|
|
|
puts foo + bar + baz
|
2021-10-22 17:54:39 +00:00
|
|
|
# ^^^ error: Shared variable `foo` is used in fiber
|
|
|
|
# ^^^ error: Shared variable `baz` is used in fiber
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
foo += 1
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_no_corrections source
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if variable is passed to the proc" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
i = 0
|
|
|
|
while i < 10
|
|
|
|
proc = ->(x : Int32) do
|
|
|
|
spawn do
|
|
|
|
puts(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
proc.call(i)
|
|
|
|
i += 1
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if a channel is declared in outer scope" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
channel = Channel(Nil).new
|
|
|
|
spawn { channel.send(nil) }
|
|
|
|
channel.receive
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if there is a loop in spawn" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
channel = Channel(String).new
|
|
|
|
|
|
|
|
spawn do
|
|
|
|
server = TCPServer.new("0.0.0.0", 8080)
|
|
|
|
socket = server.accept
|
|
|
|
while line = socket.gets
|
|
|
|
channel.send(line)
|
|
|
|
end
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if a var is mutated in spawn and referenced outside" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
def method
|
|
|
|
foo = 1
|
|
|
|
spawn { foo = 2 }
|
|
|
|
foo
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if variable is changed without iterations" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
def foo
|
|
|
|
i = 0
|
|
|
|
i += 1
|
|
|
|
spawn { i }
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if variable is in a loop inside spawn" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
i = 0
|
|
|
|
spawn do
|
|
|
|
while i < 10
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if variable declared inside loop" do
|
2021-11-06 13:15:19 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
while true
|
|
|
|
i = 0
|
|
|
|
spawn { i += 1 }
|
|
|
|
end
|
2021-11-06 13:15:19 +00:00
|
|
|
CRYSTAL
|
2020-03-24 18:10:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|