=begin #AllQ #No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) OpenAPI spec version: 1.0 Generated by: https://github.com/swagger-api/swagger-codegen.git Swagger Codegen version: 2.3.1 =end require 'spec_helper' require 'json' DEFAULT_TUBE = "default" ALT_TUBE = "" # Unit tests for Allq::ActionsApi # Automatically generated by swagger-codegen (github.com/swagger-api/swagger-codegen) # Please update as you see appropriate describe 'ActionsApi' do before do # run before each test allq_conf = Allq::Configuration.new do |config| config.host = ENV['ALLQ_CLIENT_LOCATION'] || 'localhost:8091' end raw_client = Allq::ApiClient.new(allq_conf) @instance = Allq::ActionsApi.new(raw_client) @admin_tools = Allq::AdminApi.new(raw_client) @allq_tool = AllQTools.new(@instance, @admin_tools) stats_result = @allq_tool.get_stats[0] stats_result.stats.each do |stat| @instance.tube_delete(stat.tube) end end after do # run after each test end describe 'test an instance of ActionsApi' do it 'should create an instance of ActionsApi' do expect(@instance).to be_instance_of(Allq::ActionsApi) end end # unit tests for bury_put # Bury # Bury Job # @param job_id Job ID # @param [Hash] opts the optional parameters # @return [BasicResponse] describe 'bury_put test' do it "should work" do @allq_tool.add_job expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) job = @instance.job_get(DEFAULT_TUBE) @instance.bury_put(job.id) expect(@allq_tool.get_buried(DEFAULT_TUBE)).to eq(1) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(0) end end # unit tests for job_delete # Delete # Finished Job # @param job_id Job ID # @param [Hash] opts the optional parameters # @option opts [String] :tube Name of Tube (For deleting \"ready\" objects) # @return [BasicResponse] describe 'job_delete test' do it "should work" do @allq_tool.add_job expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) job = @instance.peek_get(DEFAULT_TUBE) @instance.job_delete(job.id, tube: DEFAULT_TUBE) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(0) end end # unit tests for job_get # Job # Get job from queue # @param tube Name of tube # @param [Hash] opts the optional parameters # @return [JobResponse] describe 'job_get test' do it "should work" do @allq_tool.add_job job = @instance.job_get(DEFAULT_TUBE) expect(@allq_tool.get_reserved(DEFAULT_TUBE)).to eq(1) expect(job.id).to be_truthy end end # unit tests for multiple job_get # Job # Get job from queue # @param tube Name of tube # @param [Hash] opts the optional parameters # @return [JobResponse] describe 'mulitple_job_get test' do it "should work" do 1.upto(6) do @allq_tool.add_job end multiple_jobs = @instance.multiple_job_get(DEFAULT_TUBE, 4) expect(multiple_jobs.jobs.length).to eq(4) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(2) multiple_jobs.jobs.each do |job| @instance.job_delete(job.id) end expect(@allq_tool.get_reserved(DEFAULT_TUBE)).to eq(0) multiple_jobs = @instance.multiple_job_get(DEFAULT_TUBE, 4) expect(multiple_jobs.jobs.length).to eq(2) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(0) end end # unit tests for job_post # Job # Put job into queue # @param new_job New Job Object # @param [Hash] opts the optional parameters # @return [JobRef] describe 'job_post test' do it "should work" do @allq_tool.add_job expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) job = @instance.job_get(DEFAULT_TUBE) expect(job.id).to be_truthy end end # unit tests for parent_job_post # Parent Job # Create a parent job # @param new_parent_job New Parent Job Data # @param [Hash] opts the optional parameters # @return [JobRef] describe 'parent_job_post test' do it "should work" do new_parent_job = Allq::NewParentJob.new(tube: 'default', body: '', ttl: 1200, delay: 0, priority: 5, timeout: 1000, run_on_timeout: false, limit: 2) new_parent_job_response = @instance.parent_job_post(new_parent_job) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(1) job = @allq_tool.new_job(DEFAULT_TUBE, nil, new_parent_job_response.job_id) @instance.job_post(job) job = @allq_tool.new_job(DEFAULT_TUBE, nil, new_parent_job_response.job_id) @instance.job_post(job) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(2) @allq_tool.get_finish(DEFAULT_TUBE) @allq_tool.get_finish(DEFAULT_TUBE) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(0) @allq_tool.get_finish(DEFAULT_TUBE) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(0) end end # unit tests for parent_job_post # Parent Job # Create a parent job # @param new_parent_job New Parent Job Data # @param [Hash] opts the optional parameters # @return [JobRef] describe 'parent_job_post test with different tubes' do it "should work" do new_parent_job = Allq::NewParentJob.new(tube: 'default', body: '', ttl: 1200, delay: 0, priority: 5, timeout: 1000, run_on_timeout: false, limit: 2) new_parent_job_response = @instance.parent_job_post(new_parent_job) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(1) job = @allq_tool.new_job('foo', nil, new_parent_job_response.job_id) @instance.job_post(job) job = @allq_tool.new_job('foo', nil, new_parent_job_response.job_id) @instance.job_post(job) expect(@allq_tool.get_ready('foo')).to eq(2) @allq_tool.get_finish('foo') @allq_tool.get_finish('foo') expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(0) @allq_tool.get_finish(DEFAULT_TUBE) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(0) end end # unit tests for parent_job_post # Parent Job # Create a parent job # @param new_parent_job New Parent Job Data # @param [Hash] opts the optional parameters # @return [JobRef] describe 'parent_job_post should timeout if child not completed in time' do fit "should work" do new_parent_job = Allq::NewParentJob.new(tube: 'default', body: '', ttl: 1200, delay: 0, priority: 5, timeout: 2, run_on_timeout: true, limit: 2) new_parent_job_response = @instance.parent_job_post(new_parent_job) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(1) job = @allq_tool.new_job('foo', nil, new_parent_job_response.job_id) @instance.job_post(job) expect(@allq_tool.get_ready('foo')).to eq(1) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(1) sleep(7) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(0) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) end end # unit tests for peek_get # Peek # Peek at next job # @param tube Tube name # @param [Hash] opts the optional parameters # @option opts [String] :buried Look in buried # @return [JobResponse] describe 'peek_get test' do it "should work" do @allq_tool.add_job job = @instance.peek_get(DEFAULT_TUBE) expect(job.id).to be_truthy end end describe 'peek_get buried test' do it "should work" do @allq_tool.add_job job = @instance.peek_get(DEFAULT_TUBE, buried: true) expect(job.id).to be_falsey job = @instance.job_get(DEFAULT_TUBE) @instance.bury_put(job.id) job = @instance.peek_get(DEFAULT_TUBE, buried: true) expect(job.id).to be_truthy end end # unit tests for peek_get # Peek # Peek at next job # @param tube Tube name # @param [Hash] opts the optional parameters # @option opts [String] :buried Look in buried # @return [JobResponse] describe 'peek_get test(no jobs)' do it "should work" do job = @instance.peek_get(DEFAULT_TUBE) expect(job.id).to be_falsey end end # unit tests for release_put # Release # Releases job back into queue # @param job_id Job ID # @param [Hash] opts the optional parameters # @return [BasicResponse] describe 'release_put test' do it "should work" do @allq_tool.add_job expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) job = @instance.job_get(DEFAULT_TUBE) expect(@allq_tool.get_reserved(DEFAULT_TUBE)).to eq(1) expect(job.id).to be_truthy @instance.release_put(job.id) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) end end describe 'release should increment release' do it "should work" do @allq_tool.add_job expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) job = @instance.job_get(DEFAULT_TUBE) expect(job.releases).to eq(0) @instance.release_put(job.id) sleep(1) job = @instance.job_get(DEFAULT_TUBE) expect(job.releases).to eq(1) @instance.release_put(job.id) sleep(1) job = @instance.job_get(DEFAULT_TUBE) expect(job.releases).to eq(2) end end # unit tests for set_children_started_put # Set Children Started # When a parent job doesn't know how many children are going to be added, this is the event that sets the final children count on the parent_job, allowing it to run when the children are done. # @param job_id Job ID # @param [Hash] opts the optional parameters # @return [BasicResponse] describe 'set_children_started_put test' do it "should work" do new_parent_job = Allq::NewParentJob.new(tube: 'default', body: '', ttl: 1200, delay: 0, priority: 5, timeout: 1000, run_on_timeout: false, limit: nil) new_parent_job_response = @instance.parent_job_post(new_parent_job) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(1) job = @allq_tool.new_job(DEFAULT_TUBE, nil, new_parent_job_response.job_id) @instance.job_post(job) job = @allq_tool.new_job(DEFAULT_TUBE, nil, new_parent_job_response.job_id) @instance.job_post(job) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(2) @allq_tool.get_finish(DEFAULT_TUBE) @allq_tool.get_finish(DEFAULT_TUBE) # After jobs have already finished expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(0) # Parent still waiting expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(1) # Set limit on parent @instance.set_children_started_put(new_parent_job_response.job_id) # Now parent gets moved to ready expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) expect(@allq_tool.get_parents(DEFAULT_TUBE)).to eq(0) @allq_tool.get_finish(DEFAULT_TUBE) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(0) end end # unit tests for throttle_post # Throttle # Creates a throttle on a tube # @param throttle Throttle info # @param [Hash] opts the optional parameters # @return [TubeRef] describe 'throttle_post test' do it "should work" do throttle = Allq::Throttle.new(tps: 5, tube: "throttled") @instance.throttle_post(throttle) end end # unit tests for stats # Stats Action Count # Checks action count # @param throttle Throttle info # @param [Hash] opts the optional parameters # @return [TubeRef] describe 'throttle_post test' do it "should work" do @allq_tool.add_job expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) @instance.job_get(DEFAULT_TUBE) stats_result = @allq_tool.get_stats[0] expect(stats_result.action_count > 1).to eq(true) end end # unit tests for touch_put # Touch # Touch job # @param job_id Job ID # @param [Hash] opts the optional parameters # @return [BasicResponse] describe 'touch_put test' do it "should work" do @allq_tool.add_job expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) job = @instance.job_get(DEFAULT_TUBE) @instance.touch_put(job.id) end end # unit tests for tube_delete # Clear Tube # Delete all contents of tube # @param tube Tube Name # @param [Hash] opts the optional parameters # @return [BasicResponse] describe 'tube_delete test' do it "should work" do alt_tube_name = "alt_tube" new_job = Allq::NewJob.new(tube: alt_tube_name, body: "Some body", ttl: 1200, delay: 0, priority: 5, parent_id: nil) @instance.job_post(new_job) new_job = Allq::NewJob.new(tube: alt_tube_name, body: "Some body", ttl: 1200, delay: 0, priority: 5, parent_id: nil) @instance.job_post(new_job) new_job = Allq::NewJob.new(tube: alt_tube_name, body: "Some body", ttl: 1200, delay: 0, priority: 5, parent_id: nil) @instance.job_post(new_job) new_job = Allq::NewJob.new(tube: "alt_tube2", body: "Some body", ttl: 1200, delay: 0, priority: 5, parent_id: nil) @instance.job_post(new_job) new_job = Allq::NewJob.new(tube: "alt_tube2", body: "Some body", ttl: 1200, delay: 0, priority: 5, parent_id: nil) @instance.job_post(new_job) expect(@allq_tool.get_ready("alt_tube2")).to eq(2) # OK..it's there reserved_job = @instance.job_get("alt_tube2") # Other tube has 1 ready, 1 reserved expect(@allq_tool.get_ready("alt_tube2")).to eq(1) expect(@allq_tool.get_reserved("alt_tube2")).to eq(1) # Get 1 into buried, 1 into reserved, and 1 in ready so we can clear it expect(@allq_tool.get_ready(alt_tube_name)).to eq(3) # OK..it's there reserved_job = @instance.job_get(alt_tube_name) expect(@allq_tool.get_reserved(alt_tube_name)).to eq(1) # OK..it's there reserved_job = @instance.job_get(alt_tube_name) expect(@allq_tool.get_reserved(alt_tube_name)).to eq(2) # OK..it's there @instance.bury_put(reserved_job.id) expect(@allq_tool.get_buried(alt_tube_name)).to eq(1) # Now check that first tube is empty @instance.tube_delete(alt_tube_name) expect(@allq_tool.get_buried(alt_tube_name)).to eq(0) expect(@allq_tool.get_reserved(alt_tube_name)).to eq(0) # OK..it's there expect(@allq_tool.get_ready(alt_tube_name)).to eq(0) # ..and other tubes are fine expect(@allq_tool.get_ready("alt_tube2")).to eq(1) expect(@allq_tool.get_reserved("alt_tube2")).to eq(1) end end # unit tests for delay # Delay describe 'delay test' do it "should work" do new_job = Allq::NewJob.new(tube: DEFAULT_TUBE, body: "Some body", ttl: 1200, delay: 1, priority: 5, parent_id: nil) @instance.job_post(new_job) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(0) # Too soon to show up sleep(4) expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) # OK..it's there end end # unit tests for expire # Expire describe 'expire test' do it "should work" do new_job = Allq::NewJob.new(tube: DEFAULT_TUBE, body: "Some body", ttl: 1, delay: 0, priority: 5, parent_id: nil) @instance.job_post(new_job) sleep(1) @instance.job_get(DEFAULT_TUBE) expect(@allq_tool.get_reserved(DEFAULT_TUBE)).to eq(1) sleep(5) expect(@allq_tool.get_reserved(DEFAULT_TUBE)).to eq(0) # Should expire expect(@allq_tool.get_ready(DEFAULT_TUBE)).to eq(1) # ..and be back in ready end end end class AllQTools def initialize(action_client, admin_client) @action_client = action_client @admin_client = admin_client end def new_job(tube = 'default', body = nil, parent_id = nil) body = (0...8).map { (65 + rand(26)).chr }.join unless body new_job = Allq::NewJob.new(tube: tube, body: body, ttl: 1200, delay: 0, priority: 5, parent_id:parent_id) return new_job end def add_job(tube = DEFAULT_TUBE) client = @action_client job = new_job(tube) job = client.job_post(new_job) return job end def get_finish(tube) client = @action_client job = client.job_get(tube) client.job_delete(job.id) end def get_stats client = @admin_client client.stats_get end def get_ready(tube) stats = get_stats count = 0 find_tueq(stats, tube) do |found_stat| count += found_stat.ready.to_i end return count end def get_reserved(tube) stats = get_stats count = 0 find_tueq(stats, tube) do |found_stat| count += found_stat.reserved.to_i end return count end def get_buried(tube) stats = get_stats count = 0 find_tueq(stats, tube) do |found_stat| count += found_stat.buried.to_i end return count end def get_parents(tube) stats = get_stats count = 0 find_tueq(stats, tube) do |found_stat| count += found_stat.parents.to_i end return count end def find_tueq(all_stats, tube) all_stats.each do |server_stat| server_stat.stats.each do |stat| yield stat if stat.tube == tube end end end end