Skip to content

Commit 9495bc4

Browse files
committed
Add mutex to server requests in tests
1 parent 7f44e98 commit 9495bc4

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

test/ruby_lsp_rails/code_lens_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,7 @@ def index
401401
attr_reader :ruby
402402

403403
def generate_code_lens_for_source(source, file: "/fake.rb")
404-
with_server(source, URI(file)) do |server, uri|
405-
wait_for_rails_addon_activation
406-
404+
with_locked_server(source, URI(file)) do |server, uri|
407405
server.process_message(
408406
id: 1,
409407
method: "textDocument/codeLens",

test/ruby_lsp_rails/definition_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ def baz; end
224224
private
225225

226226
def generate_definitions_for_source(source, position)
227-
with_server(source) do |server, uri|
228-
wait_for_rails_addon_activation
229-
227+
with_locked_server(source) do |server, uri|
230228
server.process_message(
231229
id: 1,
232230
method: "textDocument/definition",

test/ruby_lsp_rails/document_symbol_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,7 @@ class FooModel < ApplicationRecord
430430
private
431431

432432
def generate_document_symbols_for_source(source)
433-
with_server(source) do |server, uri|
434-
wait_for_rails_addon_activation
435-
433+
with_locked_server(source) do |server, uri|
436434
server.process_message(
437435
id: 1,
438436
method: "textDocument/documentSymbol",

test/ruby_lsp_rails/hover_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ class User < ApplicationRecord
187187
private
188188

189189
def hover_on_source(source, position)
190-
with_server(source, stub_no_typechecker: true) do |server, uri|
191-
wait_for_rails_addon_activation
192-
190+
with_locked_server(source, stub_no_typechecker: true) do |server, uri|
193191
server.process_message(
194192
id: 1,
195193
method: "textDocument/hover",

test/test_helper.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,40 @@ class TestCase
3232
extend T::Sig
3333
include RubyLsp::TestHelper
3434

35+
@server_mutex = T.let(Mutex.new, Mutex)
36+
37+
class << self
38+
extend T::Sig
39+
40+
sig { returns(Mutex) }
41+
attr_reader :server_mutex
42+
end
43+
3544
sig { returns(String) }
3645
def dummy_root
3746
File.expand_path("#{__dir__}/dummy")
3847
end
3948

49+
# Run requests under a mutex lock to prevent add-on concurrency issues
50+
sig do
51+
type_parameters(:T)
52+
.params(
53+
source: String,
54+
uri: URI::Generic,
55+
stub_no_typechecker: T::Boolean,
56+
block: T.proc.params(server: RubyLsp::Server, uri: URI::Generic).returns(T.type_parameter(:T)),
57+
).returns(T.type_parameter(:T))
58+
end
59+
def with_locked_server(source, uri = URI("file:///fake.rb"), stub_no_typechecker: false, &block)
60+
TestCase.server_mutex.synchronize do
61+
with_server(source, uri, stub_no_typechecker: stub_no_typechecker) do |server, uri|
62+
wait_for_rails_addon_activation
63+
64+
block.call(server, uri)
65+
end
66+
end
67+
end
68+
4069
sig { void }
4170
def wait_for_rails_addon_activation
4271
Thread.new do

0 commit comments

Comments
 (0)