Skip to content

Commit

Permalink
add a writer for max_buffer_size, + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkittelson committed Nov 11, 2014
1 parent bc016d9 commit 9b7db35
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
17 changes: 12 additions & 5 deletions lib/dogstatsd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ defmodule DogStatsd do
end

def max_buffer_size(dogstatsd) do
GenServer.call(dogstatsd, :max_buffer_size)
GenServer.call(dogstatsd, :max_buffer_size) || 50
end

def max_buffer_size(dogstatsd, buffer_size) do
GenServer.call(dogstatsd, {:set_max_buffer_size, buffer_size})
end

def namespace(dogstatsd) do
Expand Down Expand Up @@ -62,10 +66,6 @@ defmodule DogStatsd do
GenServer.call(dogstatsd, {:set_tags, tags})
end

def socket(dogstatsd) do
GenServer.call(dogstatsd, :get_socket)
end

def prefix(dogstatsd) do
GenServer.call(dogstatsd, :get_prefix)
end
Expand Down Expand Up @@ -95,6 +95,13 @@ defmodule DogStatsd do
{:reply, config[:max_buffer_size], config}
end

def handle_call({:set_max_buffer_size, buffer_size}, _from, config) do
config = config
|> Map.put(:max_buffer_size, buffer_size)

{:reply, config[:max_buffer_size], config}
end

def handle_call(:get_namespace, _from, config) do
{:reply, config[:namespace], config}
end
Expand Down
17 changes: 13 additions & 4 deletions test/dogstatsd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,41 @@ defmodule DogStatsdTest do
assert DogStatsd.port(:dogstatsd) == 1234
end

test "defaults the host to 127.0.0.1, port to 8125, namespace to nil, and tags to []" do
test "defaults the host to 127.0.0.1, port to 8125, namespace to nil, tags to [] and max buffer size to 50" do
{:ok, statsd} = DogStatsd.new
assert DogStatsd.host(statsd) == "127.0.0.1"
assert DogStatsd.port(statsd) == 8125
assert DogStatsd.namespace(statsd) == nil
assert DogStatsd.tags(statsd) == []
assert DogStatsd.max_buffer_size(statsd) == 50
end

test "should be able to set host, port, namespace, and global tags" do
{:ok, statsd} = DogStatsd.new "1.3.3.7", 8126, %{:tags => ["global"], :namespace => "space"}
test "should be able to set host, port, namespace, global tags and max_buffer_size" do
{:ok, statsd} = DogStatsd.new "1.3.3.7", 8126, %{:tags => ["global"], :namespace => "space", :max_buffer_size => 25}
assert DogStatsd.host(statsd) == "1.3.3.7"
assert DogStatsd.port(statsd) == 8126
assert DogStatsd.namespace(statsd) == "space"
assert DogStatsd.tags(statsd) == ["global"]
assert DogStatsd.max_buffer_size(statsd) == 25
end


##########
# writers
##########

test "sets host, port, namespace, and global tags" do
test "sets host, port, namespace, global tags and max_buffer_size" do
DogStatsd.host(:dogstatsd, "1.2.3.4")
DogStatsd.port(:dogstatsd, 5678)
DogStatsd.namespace(:dogstatsd, "n4m35p4c3")
DogStatsd.tags(:dogstatsd, "t4g5")
DogStatsd.max_buffer_size(:dogstatsd, 25)

assert DogStatsd.host(:dogstatsd) == "1.2.3.4"
assert DogStatsd.port(:dogstatsd) == 5678
assert DogStatsd.namespace(:dogstatsd) == "n4m35p4c3"
assert DogStatsd.tags(:dogstatsd) == "t4g5"
assert DogStatsd.max_buffer_size(:dogstatsd) == 25
end

test "does not resolve hostnames to IPs" do
Expand Down Expand Up @@ -84,6 +88,11 @@ defmodule DogStatsdTest do
assert DogStatsd.tags(:dogstatsd) == []
end

test "sets nil max_buffer_size to default" do
DogStatsd.max_buffer_size(:dogstatsd, nil)
assert DogStatsd.max_buffer_size(:dogstatsd) == 50
end


###########
# increment
Expand Down

0 comments on commit 9b7db35

Please sign in to comment.