Skip to content

Commit 55293f4

Browse files
:refactor: rewrite multithread spec
1 parent 4dfe6b5 commit 55293f4

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class InertiaShareController < ApplicationController
2+
inertia_share do
3+
{ someGroupsControllerData: true }
4+
end
5+
6+
def share_without_name
7+
sleep 0.5
8+
render inertia: 'ShareTestComponent'
9+
end
10+
11+
def share_without_inertia
12+
render json: {
13+
teste: "teste"
14+
}
15+
end
16+
end

spec/dummy/config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
get 'my_location' => 'inertia_test#my_location'
2121
get 'share_multithreaded' => 'inertia_multithreaded_share#share_multithreaded'
2222
get 'share_multithreaded_error' => 'inertia_multithreaded_share#share_multithreaded_error'
23+
get 'share_without_name' => 'inertia_share#share_without_name'
24+
get 'share_without_inertia' => 'inertia_share#share_without_inertia'
2325
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2426
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2527
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'

spec/inertia/sharing_spec.rb

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,42 +54,30 @@
5454
it { is_expected.to eq props.merge({ errors: errors }) }
5555
end
5656

57-
context 'multithreaded intertia share' do
57+
context 'when multithreaded with shared data' do
5858
let(:props) { { name: 'Michael', has_goat_status: true } }
59-
it 'is expected to render props even when another thread shares Inertia data' do
60-
start_thread1 = false
61-
start_thread2 = false
6259

63-
thread1 = Thread.new do
64-
sleep 0.1 until start_thread1
65-
66-
get share_multithreaded_path, headers: {'X-Inertia' => true}
67-
expect(subject).to eq props
68-
end
60+
it 'is expected to not share data from other requests' do
61+
threads = []
62+
threads << Thread.new do
63+
# Its takes one second
64+
get share_multithreaded_path, headers: {'X-Inertia' => true}
65+
body = response.body.dup
66+
props = JSON.parse(body)['props'].deep_symbolize_keys
67+
expect(props).to eq({ name: 'Michael', has_goat_status: true })
68+
end
69+
threads << Thread.new do
70+
# Its take half a second
71+
get share_without_name_path, headers: {'X-Inertia' => true}
72+
body = response.body.dup
73+
props = JSON.parse(body)['props'].deep_symbolize_keys
74+
expect(props).to eq({ someGroupsControllerData: true })
75+
end
6976

70-
thread2 = Thread.new do
71-
sleep 0.1 until start_thread2
7277

73-
# Would prefer to make this a second get request, but RSpec will overwrite
74-
# the @response variable if another request is made in the second thread.
75-
# This simulates the relevant effects of another call to a different
76-
# controller with different values for inertia_share
77-
# TODO: Make this test work
78-
# InertiaRails.reset!
79-
# InertiaRails.share(name: 'Brian', has_goat_status: false)
78+
threads.each(&:join)
8079
end
8180

82-
# Thread 1 starts. The controller method runs inertia_share, then sleeps.
83-
# Thread 2 then modifies the shared inertia data before Thread 1 stops sleeping
84-
start_thread1 = true
85-
sleep 0.5
86-
start_thread2 = true
87-
88-
# Make sure that both threads finish before the block returns
89-
thread1.join
90-
thread2.join
91-
end
92-
9381
describe 'deep or shallow merging shared data' do
9482
context 'with default settings (shallow merge)' do
9583
describe 'shallow merging by default' do
@@ -138,7 +126,7 @@
138126
rescue Exception
139127
end
140128

141-
# TODO: Comment test
129+
# TODO: How to test this? We really neeed?
142130
# expect(InertiaRails.shared_plain_data).to be_empty
143131
# expect(InertiaRails.shared_blocks).to be_empty
144132
end

0 commit comments

Comments
 (0)