Skip to content

Instantly share code, notes, and snippets.

@glennpratt
Last active October 5, 2015 20:24
Show Gist options
  • Save glennpratt/bf254a896c9b0f15b812 to your computer and use it in GitHub Desktop.
Save glennpratt/bf254a896c9b0f15b812 to your computer and use it in GitHub Desktop.
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