Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 163 lines (132 sloc) 4.341 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
require 'spec_helper'

describe Daikon::Client, "connect" do
  subject { Daikon::Client.new(config) }

  before do
    EventMachine::Hiredis.stubs(:connect => nil)
    subject.connect
  end

  context "with overrides" do
    let(:url) { "redis://8.8.8.8:1234" }
    let(:config) { Daikon::Configuration.new(["-u", url]) }

    it "sets redis to listen on the given port" do
      EventMachine::Hiredis.should have_received(:connect).with(url).once
    end
  end

  context "with defaults" do
    let(:config) { Daikon::Configuration.new([]) }

    it "sets redis to listen on the given port" do
      EventMachine::Hiredis.should have_received(:connect).with("redis://0.0.0.0:6379").once
    end
  end
end

describe Daikon::Client, "when server is down" do
  subject { Daikon::Client.new }

  before do
    stub_request(:any, infos_url).to_timeout
  end

  it "does not kill the client" do
    em do
      subject.on(:request_error) { EM.stop }
      lambda {
        subject.report_info({})
      }.should_not raise_error
    end
  end
end

describe Daikon::Client, "when it returns bad json" do
  subject { Daikon::Client.new }

  before do
    stub_request(:post, infos_url).to_return(:body => "{'bad':'json}")
  end

  it "does not commit suicide" do
    em do
      subject.on(:request_success) { EM.stop }
      lambda {
        subject.report_info({})
      }.should_not raise_error
    end
  end
end

shared_examples_for "a summary api consumer" do
  it "shoots the results back to radish" do
    headers = {
      "Authorization" => api_key,
      "Content-Length" => payload.to_json.size.to_s,
      "Content-Type" => "application/json"
    }

    em do
      subject.rotate_monitor(DateTime.parse(past), DateTime.parse(now))
    end

    WebMock.should have_requested(:post, summaries_url(server)).
      with(:headers => headers) { |req| JSON.parse!(req.body) == payload }
  end
end

describe Daikon::Client, "rotate monitor" do
  subject { Daikon::Client.new(config) }
  let(:now) { "2011-01-19T18:23:55-05:00" }
  let(:past) { "2011-01-19T18:23:54-05:00" }
  let(:payload) do
    data.merge("start" => past, "stop" => now)
  end
  let(:data) do
    {"commands" => {"GET" => 42},
     "keys" => {"foo" => 42},
     "namespaces" => {"a" => 42, "global" => 42},
     "totals" => {"all" => 42, "read" => 42}}
  end

  before do
    Timecop.freeze DateTime.parse(now)
    Daikon::Monitor.stubs(:pop).yields(data)
    subject.on(:request_success) { EM.stop }
    stub_request(:post, summaries_url(server)).to_return(:status => 200)
  end

  after do
    Timecop.return
  end

  context "with default configuration" do
    let(:api_key) { config.api_key }
    let(:server) { "https://radish.heroku.com" }
    let(:config) { Daikon::Configuration.new }

    it_should_behave_like "a summary api consumer"
  end

  context "with custom settings" do
    let(:api_key) { "0987654321" }
    let(:server) { "http://localhost:9999" }
    let(:config) { Daikon::Configuration.new(["-k", api_key, "-s", "http://localhost:9999"]) }

    it_should_behave_like "a summary api consumer"
  end
end

shared_examples_for "a info api consumer" do
  it "shoots the results back to radish" do
    headers = {
      "Authorization" => api_key,
      "Content-Length" => info.to_json.size.to_s,
      "Content-Type" => "application/json"
    }

    em do
      subject.report_info(info)
    end

    WebMock.should have_requested(:post, infos_url(server)).
      with(:body => info.to_json, :headers => headers)
  end
end

describe Daikon::Client, "report info" do
  subject { Daikon::Client.new(config) }
  let(:info) { {"connected_clients"=>"1", "used_cpu_sys_childrens"=>"0.00"} }

  before do
    subject.on(:request_success) { EM.stop }
    stub_request(:post, infos_url(server)).to_return(:status => 200)
  end

  context "with default configuration" do
    let(:api_key) { config.api_key }
    let(:server) { "https://radish.heroku.com" }
    let(:config) { Daikon::Configuration.new }

    it_should_behave_like "a info api consumer"
  end

  context "with custom settings" do
    let(:api_key) { "0987654321" }
    let(:server) { "http://localhost:9999" }
    let(:config) { Daikon::Configuration.new(["-k", api_key, "-s", "http://localhost:9999"]) }

    it_should_behave_like "a info api consumer"
  end
end
Something went wrong with that request. Please try again.