I can't pass the test below.
"SingletonTest"
testCurrent
| semaphore object1 object2 |
Singleton initialize.
semaphore := Semaphore new.
[
Processor yield.
object1 := Singleton current.
semaphore signal ] fork.
object2 := Singleton current.
semaphore wait.
self assert: object1 identicalTo: object2
Here is my first implementation which is failed.
"Singleton class"
current
Transcript
show: 'start';
cr.
current := current ifNil: [
Transcript
show: 'creating..';
cr.
self new ].
^ current
initialize
isProcessing := false.
current := nil
I have tried to pass the test by using a naive exclusion control but the result was same. Here is my second implementation.
"Singleton class"
current
[ isProcessing ] whileTrue: [ ].
isProcessing := true.
current := current ifNil: [
(Delay forSeconds: 2) wait.
self new ].
isProcessing := false.
^ current
initialize
isProcessing := false.
current := nil
How can I pass the test? I'm looking forward to hearing your tips!
Your code for #'initialize'
resets the current
so that is why you are getting two different values. Try deleting all but the first call to #'initialize'
.
Note also that race conditions are tricky things. In the first implementation of #'current'
you have a two-second delay in which you could create two instances of Singleton
. In the second implementation you could have two threads find isProcessing
to be false
and find current
to be nil
so both wait for two seconds and create it.
To guarantee that you get only one instance, you should set up the instance during your setup code or protect the creation with a Semaphore
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With