Specs2 - Unit specification style should not be used in concurrent environments
Specs2 contributes to the functional style when dealing with the Acceptance specification (even if the Unit specification is required).
The risks of using the old style (mutable style) are mentioned in the Specs2 spec and refer to potential unwanted side effects:
Important things to know:
side effects are only used to create fragments of the specification by modifying a variable, they are also used to short-circuit the example as soon as a failure occurs (by throwing an exception). If you create snippets in the body of the examples, or do the same spec at the same time , the sky should be falling. "Context" management should be done using classes or class traits (see org.specs2.examples.MutableSpec)
I don't understand how the same spec can be executed concurrently, as each spec is different from the other (separate instances of the class), even if we run the same thing twice or more at the same time.
Indeed, specFragments
(mutable variable):
protected[mutable] var specFragments: Fragments = new Fragments()
is declared in a trait
called FragmentBuilder
, not in object
(in scala sense => singleton) or some other common thing ... so specFragments
is a local variable for each Specification
instance.
So what could a script risk the concurrency mechanism?
I really don't understand the true scenario (non-stupid) proving the advantage of Specs2's functional style.
source to share
Issues with a revised BOM can only be seen when the BOM is created, not when it is executed. It's easy to get unexpected side effects when creating a mutable BOM
import org.specs2._
val spec = new mutable.Specification {
"example" >> ok
}
import spec._
def addTitle {
// WHOOPS, forgot to remove this line!
// test, add also an example
"this is only for testing" >> ok
"new title".title
}
addTitle
And the result:
new title
+ example
+ this is only for testing
Total for specification new title
Finished in 0 ms
2 examples, 0 failure, 0 error
So you're right, the highlighted sentence in the manual ("running the same specification at the same time") is ambiguous. The design of the spec itself can be unsafe if multiple threads build the same spec object, but not if they start it (the whole process is called "execute" in this sentence).
Your other question is, what are the benefits of "functional style"? The main benefit, from a user perspective, is that it is another style of writing specs where all text comes first and all code is placed elsewhere.
In conclusion, don't be afraid of the fluid spec style if you like it!
source to share