Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/TraceClient/TraceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ private static bool IsYes(string value)
/// Reports that <paramref name="name"/> happened, with an optional numeric
/// value and optional string tags. Returns immediately and never throws.
/// The program's version is added as the tag <c>version</c> unless
/// <paramref name="tags"/> already has one; <paramref name="tags"/> itself is
/// <paramref name="tags"/> already has one, and so is <see cref="InstallId"/>, if
/// any, as the tag <c>install</c>, unless <paramref name="tags"/> has one or it
/// would pass <see cref="MaxTags"/>; <paramref name="tags"/> itself is
/// never modified. A report the server would reject for its size -- more than
/// <see cref="MaxTags"/> tags, or a string longer than <see cref="MaxLength"/> --
/// is dropped here instead of being sent.
Expand Down
40 changes: 40 additions & 0 deletions tests/TraceClient.Tests/InstallIdTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -157,6 +158,45 @@ public void Client_WithAnUnwritableFileStillReportsWithAnInMemoryId()
Assert.Contains(log, m => m.Contains("in-memory install ID"));
}

[Fact]
public void Client_WithAnUnreadableFileLogsWhyAndLeavesItAlone()
{
// A directory where the file should be: unreadable as a file on every OS.
string path = Path.Combine(_directory, "install-id");
Directory.CreateDirectory(path);
var log = new ConcurrentQueue<string>();

var client = new TraceClient(_server.BaseUrl, "MyGame", "1.2.3", key: "k", log: log.Enqueue,
installIdFile: path);
client.Report("startup");
client.Close();

Assert.True(IsGuid(client.InstallId), client.InstallId);
Assert.True(Directory.Exists(path));
// The prefix is doubled today (#21); this pins it until that is fixed.
Assert.Contains(log, m => m.StartsWith("[trace] [trace] using an in-memory install ID for this process: could not read " + path + ": "));
Assert.DoesNotContain(log, m => m.Contains("could not write"));
Assert.Contains("\"install\":\"" + client.InstallId + "\"", _server.Received.Single().Body);
}

[Fact]
public void Client_WhoseIdFileFailsStillConstructsWhenTheLoggerThrows()
{
string path = Path.Combine(_directory, "install-id");
Directory.CreateDirectory(path);
int calls = 0;

var client = new TraceClient(_server.BaseUrl, "MyGame", "1.2.3", key: "k",
log: m => { Interlocked.Increment(ref calls); throw new InvalidOperationException("logger broke"); },
installIdFile: path);
client.Report("startup");
client.Close();

Assert.True(calls > 0); // the failure was offered to the logger
Assert.True(IsGuid(client.InstallId), client.InstallId);
Assert.Contains("\"install\":\"" + client.InstallId + "\"", _server.Received.Single().Body);
}

[Fact]
public void DisabledClient_NeverMakesUpOrWritesAnId()
{
Expand Down
Loading