@@ -20,6 +20,41 @@ def fiber_operation(value)
20
20
end . resume
21
21
end
22
22
23
+ context 'when set to an unsupported value' do
24
+ it 'raises an error' do
25
+ old_value = Mongoid ::Config . isolation_level
26
+ expect { Mongoid ::Config . isolation_level = :unsupported }
27
+ . to raise_error ( Mongoid ::Errors ::UnsupportedIsolationLevel )
28
+ expect ( Mongoid ::Config . isolation_level ) . to eq ( old_value )
29
+ end
30
+ end
31
+
32
+ context 'when using older Ruby' do
33
+ ruby_version_lt '3.2'
34
+
35
+ context 'when set to :fiber' do
36
+ it 'raises an error' do
37
+ expect { Mongoid ::Config . isolation_level = :fiber }
38
+ . to raise_error ( Mongoid ::Errors ::UnsupportedIsolationLevel )
39
+ end
40
+ end
41
+
42
+ context 'when set to :thread' do
43
+ around do |example |
44
+ save = Mongoid ::Config . isolation_level
45
+ example . run
46
+ ensure
47
+ Mongoid ::Config . isolation_level = save
48
+ end
49
+
50
+ it 'sets the isolation level' do
51
+ expect { Mongoid ::Config . isolation_level = :thread }
52
+ . not_to raise_error
53
+ expect ( Mongoid ::Config . isolation_level ) . to eq ( :thread )
54
+ end
55
+ end
56
+ end
57
+
23
58
context 'when set to :thread' do
24
59
config_override :isolation_level , :thread
25
60
@@ -42,54 +77,58 @@ def fiber_operation(value)
42
77
end
43
78
end
44
79
45
- context 'when set to :fiber ' do
46
- config_override :isolation_level , :fiber
80
+ context 'when using Ruby 3.2+ ' do
81
+ ruby_version_gte '3.2'
47
82
48
- context 'when operating inside threads ' do
49
- let ( :result ) { fiber_operation ( 'a' ) { thread_operation ( 'b' ) } }
83
+ context 'when set to :fiber ' do
84
+ config_override :isolation_level , :fiber
50
85
51
- it 'exposes the fiber state within the thread' do
52
- expect ( result ) . to eq ( %w[ a b ] )
86
+ context 'when operating inside threads' do
87
+ let ( :result ) { fiber_operation ( 'a' ) { thread_operation ( 'b' ) } }
88
+
89
+ it 'exposes the fiber state within the thread' do
90
+ expect ( result ) . to eq ( %w[ a b ] )
91
+ end
53
92
end
54
- end
55
93
56
- context 'when operating in nested fibers' do
57
- let ( :result ) { fiber_operation ( 'a' ) { fiber_operation ( 'b' ) } }
94
+ context 'when operating in nested fibers' do
95
+ let ( :result ) { fiber_operation ( 'a' ) { fiber_operation ( 'b' ) } }
58
96
59
- it 'propagates fiber state to nested fibers' do
60
- expect ( result ) . to eq ( %w[ a b ] )
97
+ it 'propagates fiber state to nested fibers' do
98
+ expect ( result ) . to eq ( %w[ a b ] )
99
+ end
61
100
end
62
- end
63
101
64
- context 'when operating in adjacent fibers' do
65
- let ( :result1 ) { fiber_operation ( 'a' ) { fiber_operation ( 'b' ) } }
66
- let ( :result2 ) { fiber_operation ( 'c' ) { fiber_operation ( 'd' ) } }
102
+ context 'when operating in adjacent fibers' do
103
+ let ( :result1 ) { fiber_operation ( 'a' ) { fiber_operation ( 'b' ) } }
104
+ let ( :result2 ) { fiber_operation ( 'c' ) { fiber_operation ( 'd' ) } }
67
105
68
- it 'maintains isolation between adjacent fibers' do
69
- expect ( result1 ) . to eq ( %w[ a b ] )
70
- expect ( result2 ) . to eq ( %w[ c d ] )
106
+ it 'maintains isolation between adjacent fibers' do
107
+ expect ( result1 ) . to eq ( %w[ a b ] )
108
+ expect ( result2 ) . to eq ( %w[ c d ] )
109
+ end
71
110
end
72
- end
73
111
74
- describe '#reset!' do
75
- context 'when operating in nested fibers' do
76
- let ( :result ) do
77
- fiber_operation ( 'a' ) do
78
- Mongoid ::Threaded . reset!
79
-
80
- # once reset, subsequent nested fibers will each have their own
81
- # state; they won't touch the reset state here.
82
- fiber_operation ( 'b' )
83
- fiber_operation ( 'c' )
84
-
85
- # If we then add to the stack here, it will be unaffected by
86
- # the previous fiber operations.
87
- Mongoid ::Threaded . stack ( :testing ) << 'd'
112
+ describe '#reset!' do
113
+ context 'when operating in nested fibers' do
114
+ let ( :result ) do
115
+ fiber_operation ( 'a' ) do
116
+ Mongoid ::Threaded . reset!
117
+
118
+ # once reset, subsequent nested fibers will each have their own
119
+ # state; they won't touch the reset state here.
120
+ fiber_operation ( 'b' )
121
+ fiber_operation ( 'c' )
122
+
123
+ # If we then add to the stack here, it will be unaffected by
124
+ # the previous fiber operations.
125
+ Mongoid ::Threaded . stack ( :testing ) << 'd'
126
+ end
88
127
end
89
- end
90
128
91
- it 'clears the fiber state' do
92
- expect ( result ) . to eq ( %w[ d ] )
129
+ it 'clears the fiber state' do
130
+ expect ( result ) . to eq ( %w[ d ] )
131
+ end
93
132
end
94
133
end
95
134
end
0 commit comments