Last active
October 5, 2015 20:24
-
-
Save glennpratt/bf254a896c9b0f15b812 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json-schema' | |
require 'json-schema/validator' | |
require 'thread' | |
require 'thwait' | |
schema = { | |
"$schema" => "http://json-schema.org/draft-03/schema#", | |
"properties" => { | |
"a" => {"type" => "string", "required" => true}, | |
"b" => {"type" => "string"} | |
} | |
} | |
validator = JSON::Validator.new(schema, nil, record_errors: true) | |
# thread safety test | |
times = 5_000 | |
threads = times.times.map do |x| | |
Thread.new(x) do |x| | |
Thread.current.abort_on_exception = true | |
case x % 2 | |
when 0 | |
errors = validator.validate_data({"b" => "a"}) | |
fail "{} should not validate." if errors.empty? | |
when 1 | |
errors = validator.validate_data({"a" => "a", "b" => "b"}) | |
fail "{} should not validate." unless errors.empty? | |
end | |
end | |
end | |
ThreadsWait.all_waits(threads) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment