Skip to content

Commit

Permalink
Misc improvements (#12)
Browse files Browse the repository at this point in the history
* Add a test for Event.Clone().
* Do add dropped responses to the Responses queue.
* Make Response sealed and its .ctor internal.
* Proper handling of LibHoney disposal under EventTest.
* Have an internal ctor for easily creating single threaded LibHoneys for testing.
  • Loading branch information
carlosalberto authored Jun 7, 2017
1 parent 0470cb6 commit 4a7aa48
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
54 changes: 52 additions & 2 deletions Src/LibHoney.Tests/EventTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ public void Ctor ()
Assert.Equal (honey.SampleRate, ev.SampleRate);
}

[Fact]
public void Clone ()
{
var ev = new Event (GetLibHoney (),
new Dictionary<string, object> () {
["hello"] = "honey"
},
new Dictionary<string, Func<object>> () {
["dynamic_hello"] = () => "dynamic_honey"
}) {
Metadata = new object ()
};
var clone = ev.Clone ();

Assert.NotSame (ev, clone);
Assert.Equal (ev.WriteKey, clone.WriteKey);
Assert.Equal (ev.DataSet, clone.DataSet);
Assert.Equal (ev.ApiHost, clone.ApiHost);
Assert.Equal (ev.SampleRate, clone.SampleRate);
Assert.Equal (ev.Metadata, clone.Metadata);
Assert.Equal (ev.CreatedAt, clone.CreatedAt);

Assert.NotSame (ev.Fields, clone.Fields);
Assert.Equal (ev.Fields.IsEmpty, ev.Fields.IsEmpty);
Assert.Equal (ev.Fields.Fields.Count, ev.Fields.Fields.Count);
}

[Fact]
public void AddNull ()
{
Expand Down Expand Up @@ -115,7 +142,8 @@ public void Metadata ()
[Fact]
public void SendDisposed ()
{
var honey = GetLibHoney ();
// Create our own LibHoney so we can dispose it right away.
var honey = new LibHoney ("key1", "data1");
var ev = new Event (honey);
honey.Dispose ();

Expand All @@ -136,7 +164,8 @@ public void SendEmpty ()
[Fact]
public void SendPreSampledDisposed ()
{
var honey = GetLibHoney ();
// Create our own LibHoney so we can dispose it right away.
var honey = new LibHoney ("key1", "data1");
var ev = new Event (honey);
honey.Dispose ();

Expand All @@ -154,6 +183,27 @@ public void SendPreSampledEmpty ()
Assert.True (excThrown);
}

[Fact]
public void SendDropped ()
{
var honey = GetLibHoney ();

var ev = new Event (honey) {
Metadata = new object (),
SampleRate = Int32.MaxValue - 1
};
ev.Send ();
Assert.Equal (1, honey.Responses.Count);

Response res;
honey.Responses.TryTake (out res);
Assert.Equal ("Event dropped due to sampling", res.ErrorMessage);
Assert.Equal (ev.Metadata, res.Metadata);
Assert.Equal (TimeSpan.Zero, res.Duration);
Assert.Equal (0, (int) res.StatusCode);
Assert.Null (res.Body);
}

[Fact]
public void ToJSONBasic ()
{
Expand Down
18 changes: 9 additions & 9 deletions Src/LibHoney.Tests/HoneyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void Ctor4 ()
[Fact]
public void AddNull ()
{
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney", 1);

bool excThrown = false;
try { libHoney.Add (null); } catch (ArgumentNullException) { excThrown = true; }
Expand All @@ -174,7 +174,7 @@ public void AddNull ()
[Fact]
public void AddFieldNull ()
{
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney", 1);

bool excThrown = false;
try { libHoney.AddField (null, "abc"); } catch (ArgumentNullException) { excThrown = true; }
Expand All @@ -188,7 +188,7 @@ public void AddFieldNull ()
[Fact]
public void AddDynamicFieldNull ()
{
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney", 1);

bool excThrown = false;
try { libHoney.AddDynamicField (null, () => "abc"); } catch (ArgumentNullException) { excThrown = true; }
Expand All @@ -213,7 +213,7 @@ public void AfterDispose ()
[Fact]
public void DisposeMultiple ()
{
var libHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = new LibHoney ("key1", "HelloHoney", 1);
libHoney.Dispose ();

// Again, a few times.
Expand All @@ -224,7 +224,7 @@ public void DisposeMultiple ()
[Fact]
public void SendNowNull ()
{
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney", 1);

bool excThrown = false;
try { libHoney.SendNow (null); } catch (ArgumentNullException) { excThrown = true; }
Expand All @@ -234,7 +234,7 @@ public void SendNowNull ()
[Fact]
public void SendNowDisposed ()
{
var libHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = new LibHoney ("key1", "HelloHoney", 1);
libHoney.Dispose ();

bool excThrown = false;
Expand All @@ -245,7 +245,7 @@ public void SendNowDisposed ()
[Fact]
public void SendEmpty ()
{
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney", 1);

bool excThrown = false;
try { libHoney.SendNow (new Dictionary<string, object> ()); } catch (SendException) { excThrown = true; }
Expand All @@ -255,7 +255,7 @@ public void SendEmpty ()
[Fact]
public void InternalStateAfterInit ()
{
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney", 1);
libHoney.AddField ("counter", 13);
libHoney.AddField ("value", DateTime.Now);
libHoney.AddDynamicField ("dynamic_value", () => DateTime.Now);
Expand All @@ -272,7 +272,7 @@ public void InternalStateAfterInit ()
[Fact]
public void InternalStateAfterDispose ()
{
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney");
var libHoney = LibHoney = new LibHoney ("key1", "HelloHoney", 1);
libHoney.Dispose ();

Assert.Equal (true, libHoney.IsDisposed);
Expand Down
4 changes: 2 additions & 2 deletions Src/LibHoney.Tests/LibHoneyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace Honeycomb.Tests
{
public class LibHoneyFixture
public class LibHoneyFixture : IDisposable
{
public LibHoneyFixture ()
{
LibHoney = new LibHoney ("key1", "HelloTest");
LibHoney = new LibHoney ("key1", "HelloTest", 1);
}

public void Dispose ()
Expand Down
5 changes: 4 additions & 1 deletion Src/LibHoney/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ public void SendPreSampled ()

void SendDroppedResponse ()
{
// XXX (calberto) Add the response object to the responses queue.
libHoney.Responses.Add (new Response () {
Metadata = Metadata,
ErrorMessage = "Event dropped due to sampling"
});
}

static bool ShouldDrop (int rate)
Expand Down
6 changes: 6 additions & 0 deletions Src/LibHoney/LibHoney.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public LibHoney (string writeKey, string dataSet, string apiHost, int sampleRate
BlockOnResponse = blockOnResponse;
}

public LibHoney (string writeKey, string dataSet, int maxConcurrentBatches)
: this (writeKey, dataSet, DefaultApiHost, DefaultSampleRate, maxConcurrentBatches,
DefaultBlock, DefaultBlock)
{
}

internal void Reset ()
{
fields.Clear ();
Expand Down
6 changes: 5 additions & 1 deletion Src/LibHoney/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

namespace Honeycomb
{
public class Response
public sealed class Response
{
internal Response ()
{
}

public HttpStatusCode StatusCode {
get;
internal set;
Expand Down

0 comments on commit 4a7aa48

Please sign in to comment.