From f811a113021ce037ca25ec3d329910052beac714 Mon Sep 17 00:00:00 2001 From: Sergey Kanzhelev Date: Wed, 2 Jan 2019 14:45:55 -0800 Subject: [PATCH] IList -> IEnumerable (few of them) (#84) * few IList replaced with IEnumerable * parent links from IList to IEnumerable * a few more of IList -> IEnumerable * one more * stats event with IEnumerable now * smal utils method should not use IList * another utility method --- .../Trace/Export/IHandler.cs | 2 +- src/OpenCensus.Abstractions/Trace/ISampler.cs | 2 +- .../Trace/ISpanBuilder.cs | 2 +- .../Trace/Sampler/AlwaysSampleSampler.cs | 2 +- .../Trace/Sampler/NeverSampleSampler.cs | 2 +- .../Trace/Sampler/ProbabilitySampler.cs | 2 +- .../Implementation/TraceExporterHandler.cs | 2 +- .../Implementation/TraceExporterHandler.cs | 2 +- .../StackdriverTraceExporter.cs | 2 +- .../Implementation/TraceExporterHandler.cs | 2 +- src/OpenCensus/Stats/MeasureMapBuilder.cs | 2 +- src/OpenCensus/Stats/MeasureToViewMap.cs | 4 +-- src/OpenCensus/Stats/StatsExtensions.cs | 19 ++++++----- src/OpenCensus/Stats/StatsManager.cs | 6 ++-- src/OpenCensus/Trace/Export/LinkList.cs | 2 +- src/OpenCensus/Trace/Export/SpanData.cs | 2 +- .../Trace/Export/SpanExporterWorker.cs | 2 +- src/OpenCensus/Trace/Export/TimedEvents.cs | 2 +- src/OpenCensus/Trace/NoopSpanBuilder.cs | 2 +- src/OpenCensus/Trace/Span.cs | 5 ++- src/OpenCensus/Trace/SpanBuilder.cs | 15 ++++---- src/OpenCensus/Trace/SpanBuilderBase.cs | 13 +------ src/OpenCensus/Utils/Collections.cs | 6 ++-- .../Impl/Stats/MeasureMapBuilderTest.cs | 12 +++---- .../Impl/Stats/StatsTestUtil.cs | 34 +++++++++++-------- .../Impl/Testing/Export/TestHandler.cs | 10 +++--- .../Impl/Trace/Export/SpanExporterTest.cs | 27 ++++++++------- .../Impl/Trace/Sampler/SamplersTest.cs | 2 +- 28 files changed, 91 insertions(+), 94 deletions(-) diff --git a/src/OpenCensus.Abstractions/Trace/Export/IHandler.cs b/src/OpenCensus.Abstractions/Trace/Export/IHandler.cs index 911d81b..bcfbda9 100644 --- a/src/OpenCensus.Abstractions/Trace/Export/IHandler.cs +++ b/src/OpenCensus.Abstractions/Trace/Export/IHandler.cs @@ -27,6 +27,6 @@ public interface IHandler /// Exports the list of spans to the backend. /// /// Collection of spans to export. - void Export(IList spanDataList); + void Export(IEnumerable spanDataList); } } diff --git a/src/OpenCensus.Abstractions/Trace/ISampler.cs b/src/OpenCensus.Abstractions/Trace/ISampler.cs index 65e3b7b..721119f 100644 --- a/src/OpenCensus.Abstractions/Trace/ISampler.cs +++ b/src/OpenCensus.Abstractions/Trace/ISampler.cs @@ -42,6 +42,6 @@ public interface ISampler /// /// Links associated with the parent span. /// True of span needs to be created. False otherwise. - bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IList parentLinks); + bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable parentLinks); } } diff --git a/src/OpenCensus.Abstractions/Trace/ISpanBuilder.cs b/src/OpenCensus.Abstractions/Trace/ISpanBuilder.cs index 2fcbd1e..187b4b2 100644 --- a/src/OpenCensus.Abstractions/Trace/ISpanBuilder.cs +++ b/src/OpenCensus.Abstractions/Trace/ISpanBuilder.cs @@ -36,7 +36,7 @@ public interface ISpanBuilder /// /// Parent links to set on span. /// This span builder for chaining. - ISpanBuilder SetParentLinks(IList parentLinks); + ISpanBuilder SetParentLinks(IEnumerable parentLinks); /// /// Set the record events value. diff --git a/src/OpenCensus.Abstractions/Trace/Sampler/AlwaysSampleSampler.cs b/src/OpenCensus.Abstractions/Trace/Sampler/AlwaysSampleSampler.cs index 052f08b..f9abd4c 100644 --- a/src/OpenCensus.Abstractions/Trace/Sampler/AlwaysSampleSampler.cs +++ b/src/OpenCensus.Abstractions/Trace/Sampler/AlwaysSampleSampler.cs @@ -32,7 +32,7 @@ public string Description } } - public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IList parentLinks) + public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable parentLinks) { return true; } diff --git a/src/OpenCensus.Abstractions/Trace/Sampler/NeverSampleSampler.cs b/src/OpenCensus.Abstractions/Trace/Sampler/NeverSampleSampler.cs index a48233a..61e1ae1 100644 --- a/src/OpenCensus.Abstractions/Trace/Sampler/NeverSampleSampler.cs +++ b/src/OpenCensus.Abstractions/Trace/Sampler/NeverSampleSampler.cs @@ -32,7 +32,7 @@ public string Description } } - public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IList parentLinks) + public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable parentLinks) { return false; } diff --git a/src/OpenCensus.Abstractions/Trace/Sampler/ProbabilitySampler.cs b/src/OpenCensus.Abstractions/Trace/Sampler/ProbabilitySampler.cs index 735820f..6b15939 100644 --- a/src/OpenCensus.Abstractions/Trace/Sampler/ProbabilitySampler.cs +++ b/src/OpenCensus.Abstractions/Trace/Sampler/ProbabilitySampler.cs @@ -40,7 +40,7 @@ public string Description public long IdUpperBound { get; } - public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IList parentLinks) + public bool ShouldSample(ISpanContext parentContext, bool hasRemoteParent, ITraceId traceId, ISpanId spanId, string name, IEnumerable parentLinks) { // If the parent is sampled keep the sampling decision. if (parentContext != null && parentContext.TraceOptions.IsSampled) diff --git a/src/OpenCensus.Exporter.ApplicationInsights/Implementation/TraceExporterHandler.cs b/src/OpenCensus.Exporter.ApplicationInsights/Implementation/TraceExporterHandler.cs index f386cfe..b22affc 100644 --- a/src/OpenCensus.Exporter.ApplicationInsights/Implementation/TraceExporterHandler.cs +++ b/src/OpenCensus.Exporter.ApplicationInsights/Implementation/TraceExporterHandler.cs @@ -34,7 +34,7 @@ public TraceExporterHandler(TelemetryConfiguration telemetryConfiguration) this.telemetryClient = new TelemetryClient(telemetryConfiguration); } - public void Export(IList spanDataList) + public void Export(IEnumerable spanDataList) { foreach (var span in spanDataList) { diff --git a/src/OpenCensus.Exporter.Ocagent/Implementation/TraceExporterHandler.cs b/src/OpenCensus.Exporter.Ocagent/Implementation/TraceExporterHandler.cs index bb6202a..979d5d5 100644 --- a/src/OpenCensus.Exporter.Ocagent/Implementation/TraceExporterHandler.cs +++ b/src/OpenCensus.Exporter.Ocagent/Implementation/TraceExporterHandler.cs @@ -72,7 +72,7 @@ public TraceExporterHandler(string agentEndpoint, string hostName, string servic this.Start(); } - public void Export(IList spanDataList) + public void Export(IEnumerable spanDataList) { if (this.cts == null || this.cts.IsCancellationRequested) { diff --git a/src/OpenCensus.Exporter.Stackdriver/Implementation/StackdriverTraceExporter.cs b/src/OpenCensus.Exporter.Stackdriver/Implementation/StackdriverTraceExporter.cs index 7469116..be4bde0 100644 --- a/src/OpenCensus.Exporter.Stackdriver/Implementation/StackdriverTraceExporter.cs +++ b/src/OpenCensus.Exporter.Stackdriver/Implementation/StackdriverTraceExporter.cs @@ -182,7 +182,7 @@ static StackdriverTraceExporter() } } - public void Export(IList spanDataList) + public void Export(IEnumerable spanDataList) { TraceServiceClient traceWriter = TraceServiceClient.Create(settings: traceServiceSettings); diff --git a/src/OpenCensus.Exporter.Zipkin/Implementation/TraceExporterHandler.cs b/src/OpenCensus.Exporter.Zipkin/Implementation/TraceExporterHandler.cs index 6b80c0b..ae790d9 100644 --- a/src/OpenCensus.Exporter.Zipkin/Implementation/TraceExporterHandler.cs +++ b/src/OpenCensus.Exporter.Zipkin/Implementation/TraceExporterHandler.cs @@ -46,7 +46,7 @@ public TraceExporterHandler(ZipkinTraceExporterOptions options, HttpClient clien this.httpClient = client ?? new HttpClient(); } - public void Export(IList spanDataList) + public void Export(IEnumerable spanDataList) { List zipkinSpans = new List(); diff --git a/src/OpenCensus/Stats/MeasureMapBuilder.cs b/src/OpenCensus/Stats/MeasureMapBuilder.cs index 8d21ddf..37d2e11 100644 --- a/src/OpenCensus/Stats/MeasureMapBuilder.cs +++ b/src/OpenCensus/Stats/MeasureMapBuilder.cs @@ -41,7 +41,7 @@ internal MeasureMapBuilder Put(IMeasureLong measure, long value) return this; } - internal IList Build() + internal IEnumerable Build() { // Note: this makes adding measurements quadratic but is fastest for the sizes of // MeasureMapInternals that we should see. We may want to go to a strategy of sort/eliminate diff --git a/src/OpenCensus/Stats/MeasureToViewMap.cs b/src/OpenCensus/Stats/MeasureToViewMap.cs index 303d138..cbbfaef 100644 --- a/src/OpenCensus/Stats/MeasureToViewMap.cs +++ b/src/OpenCensus/Stats/MeasureToViewMap.cs @@ -103,7 +103,7 @@ internal void RegisterView(IView view, IClock clock) } // Records stats with a set of tags. - internal void Record(ITagContext tags, IList stats, ITimestamp timestamp) + internal void Record(ITagContext tags, IEnumerable stats, ITimestamp timestamp) { lock (this.lck) { @@ -117,7 +117,7 @@ internal void Record(ITagContext tags, IList stats, ITimestamp tim continue; } - IList views = this.mutableMap[measure.Name]; + var views = this.mutableMap[measure.Name]; foreach (MutableViewData view in views) { measurement.Match( diff --git a/src/OpenCensus/Stats/StatsExtensions.cs b/src/OpenCensus/Stats/StatsExtensions.cs index b038493..271c89e 100644 --- a/src/OpenCensus/Stats/StatsExtensions.cs +++ b/src/OpenCensus/Stats/StatsExtensions.cs @@ -18,6 +18,7 @@ namespace OpenCensus.Stats { using System; using System.Collections.Generic; + using System.Linq; using OpenCensus.Tags; public static class StatsExtensions @@ -67,30 +68,32 @@ public static IAggregationData Sum(this IDictionary return MutableViewData.CreateAggregationData(sum, view.Measure); } - private static bool TagValuesMatch(IList aggValues, IList values) + private static bool TagValuesMatch(IEnumerable aggValues, IEnumerable values) { if (values == null) { return true; } - if (aggValues.Count != values.Count) + if (aggValues.Count() != values.Count()) { return false; } - for (int i = 0; i < aggValues.Count; i++) - { - var v1 = aggValues[i]; - var v2 = values[i]; + var first = aggValues.GetEnumerator(); + var second = values.GetEnumerator(); + while (first.MoveNext()) + { + second.MoveNext(); + // Null matches any aggValue - if (v2 == null) + if (second.Current == null) { continue; } - if (!v2.Equals(v1)) + if (first.Current != second.Current) { return false; } diff --git a/src/OpenCensus/Stats/StatsManager.cs b/src/OpenCensus/Stats/StatsManager.cs index 9583038..b94743b 100644 --- a/src/OpenCensus/Stats/StatsManager.cs +++ b/src/OpenCensus/Stats/StatsManager.cs @@ -57,7 +57,7 @@ internal IViewData GetView(IViewName viewName) return this.measureToViewMap.GetView(viewName, this.clock, this.state.Internal); } - internal void Record(ITagContext tags, IList measurementValues) + internal void Record(ITagContext tags, IEnumerable measurementValues) { // TODO(songya): consider exposing No-op MeasureMap and use it when stats state is DISABLED, so // that we don't need to create actual MeasureMapImpl. @@ -80,10 +80,10 @@ internal void ResumeStatsCollection() private class StatsEvent : IEventQueueEntry { private readonly ITagContext tags; - private readonly IList stats; + private readonly IEnumerable stats; private readonly StatsManager statsManager; - public StatsEvent(StatsManager statsManager, ITagContext tags, IList stats) + public StatsEvent(StatsManager statsManager, ITagContext tags, IEnumerable stats) { this.statsManager = statsManager; this.tags = tags; diff --git a/src/OpenCensus/Trace/Export/LinkList.cs b/src/OpenCensus/Trace/Export/LinkList.cs index 9793ed7..072ef39 100644 --- a/src/OpenCensus/Trace/Export/LinkList.cs +++ b/src/OpenCensus/Trace/Export/LinkList.cs @@ -53,7 +53,7 @@ public override string ToString() + "}"; } - /// + /// public override bool Equals(object o) { if (o == this) diff --git a/src/OpenCensus/Trace/Export/SpanData.cs b/src/OpenCensus/Trace/Export/SpanData.cs index ea8b31f..8a92d25 100644 --- a/src/OpenCensus/Trace/Export/SpanData.cs +++ b/src/OpenCensus/Trace/Export/SpanData.cs @@ -100,7 +100,7 @@ public static ISpanData Create( throw new ArgumentNullException(nameof(messageOrNetworkEvents)); } - IList> messageEventsList = new List>(); + var messageEventsList = new List>(); foreach (ITimedEvent timedEvent in messageOrNetworkEvents.Events) { messageEventsList.Add(timedEvent); diff --git a/src/OpenCensus/Trace/Export/SpanExporterWorker.cs b/src/OpenCensus/Trace/Export/SpanExporterWorker.cs index 91dd0f2..f586be9 100644 --- a/src/OpenCensus/Trace/Export/SpanExporterWorker.cs +++ b/src/OpenCensus/Trace/Export/SpanExporterWorker.cs @@ -128,7 +128,7 @@ private void BuildList(ISpan item, List toExport) } } - private void Export(IList export) + private void Export(IEnumerable export) { var handlers = this.serviceHandlers.Values; foreach (var handler in handlers) diff --git a/src/OpenCensus/Trace/Export/TimedEvents.cs b/src/OpenCensus/Trace/Export/TimedEvents.cs index 095c7b6..189eddb 100644 --- a/src/OpenCensus/Trace/Export/TimedEvents.cs +++ b/src/OpenCensus/Trace/Export/TimedEvents.cs @@ -32,7 +32,7 @@ internal TimedEvents(IList> events, int droppedEventsCount) public int DroppedEventsCount { get; } - public static ITimedEvents Create(IList> events, int droppedEventsCount) + public static ITimedEvents Create(IEnumerable> events, int droppedEventsCount) { if (events == null) { diff --git a/src/OpenCensus/Trace/NoopSpanBuilder.cs b/src/OpenCensus/Trace/NoopSpanBuilder.cs index 01f7618..e9c0153 100644 --- a/src/OpenCensus/Trace/NoopSpanBuilder.cs +++ b/src/OpenCensus/Trace/NoopSpanBuilder.cs @@ -39,7 +39,7 @@ public override ISpanBuilder SetSampler(ISampler sampler) return this; } - public override ISpanBuilder SetParentLinks(IList parentLinks) + public override ISpanBuilder SetParentLinks(IEnumerable parentLinks) { return this; } diff --git a/src/OpenCensus/Trace/Span.cs b/src/OpenCensus/Trace/Span.cs index 564c331..3be8562 100644 --- a/src/OpenCensus/Trace/Span.cs +++ b/src/OpenCensus/Trace/Span.cs @@ -466,16 +466,15 @@ internal static ISpan StartSpan( return span; } - // public abstract void AddLink(LinkBase link); private static ITimedEvents CreateTimedEvents(TraceEvents> events, ITimestampConverter timestampConverter) { if (events == null) { - IList> empty = new List>(); + IEnumerable> empty = new ITimedEvent[0]; return TimedEvents.Create(empty, 0); } - IList> eventsList = new List>(events.Events.Count); + var eventsList = new List>(events.Events.Count); foreach (EventWithNanoTime networkEvent in events.Events) { eventsList.Add(networkEvent.ToSpanDataTimedEvent(timestampConverter)); diff --git a/src/OpenCensus/Trace/SpanBuilder.cs b/src/OpenCensus/Trace/SpanBuilder.cs index 1becfba..0c8908c 100644 --- a/src/OpenCensus/Trace/SpanBuilder.cs +++ b/src/OpenCensus/Trace/SpanBuilder.cs @@ -18,6 +18,7 @@ namespace OpenCensus.Trace { using System; using System.Collections.Generic; + using System.Linq; using OpenCensus.Internal; using OpenCensus.Trace.Config; using OpenCensus.Trace.Internal; @@ -46,7 +47,7 @@ private SpanBuilder(string name, SpanBuilderOptions options, ISpanContext remote private ISampler Sampler { get; set; } - private IList ParentLinks { get; set; } = new List(); + private IEnumerable ParentLinks { get; set; } = new List(); private bool RecordEvents { get; set; } @@ -94,7 +95,7 @@ public override ISpanBuilder SetSampler(ISampler sampler) return this; } - public override ISpanBuilder SetParentLinks(IList parentLinks) + public override ISpanBuilder SetParentLinks(IEnumerable parentLinks) { this.ParentLinks = parentLinks ?? throw new ArgumentNullException(nameof(parentLinks)); return this; @@ -116,7 +117,7 @@ internal static ISpanBuilder CreateWithRemoteParent(string spanName, ISpanContex return new SpanBuilder(spanName, options, remoteParentSpanContext, null); } - private static bool IsAnyParentLinkSampled(IList parentLinks) + private static bool IsAnyParentLinkSampled(IEnumerable parentLinks) { foreach (ISpan parentLink in parentLinks) { @@ -129,9 +130,9 @@ private static bool IsAnyParentLinkSampled(IList parentLinks) return false; } - private static void LinkSpans(ISpan span, IList parentLinks) + private static void LinkSpans(ISpan span, IEnumerable parentLinks) { - if (parentLinks.Count > 0) + if (parentLinks.Any()) { ILink childLink = Link.FromSpanContext(span.Context, LinkType.ChildLinkedSpan); foreach (ISpan linkedSpan in parentLinks) @@ -147,7 +148,7 @@ private static bool MakeSamplingDecision( bool hasRemoteParent, string name, ISampler sampler, - IList parentLinks, + IEnumerable parentLinks, ITraceId traceId, ISpanId spanId, ITraceParams activeTraceParams) @@ -176,7 +177,7 @@ private ISpan StartSpanInternal( bool hasRemoteParent, string name, ISampler sampler, - IList parentLinks, + IEnumerable parentLinks, bool recordEvents, ITimestampConverter timestampConverter) { diff --git a/src/OpenCensus/Trace/SpanBuilderBase.cs b/src/OpenCensus/Trace/SpanBuilderBase.cs index 73f9ec2..b97fcc1 100644 --- a/src/OpenCensus/Trace/SpanBuilderBase.cs +++ b/src/OpenCensus/Trace/SpanBuilderBase.cs @@ -23,7 +23,7 @@ public abstract class SpanBuilderBase : ISpanBuilder { public abstract ISpanBuilder SetSampler(ISampler sampler); - public abstract ISpanBuilder SetParentLinks(IList parentLinks); + public abstract ISpanBuilder SetParentLinks(IEnumerable parentLinks); public abstract ISpanBuilder SetRecordEvents(bool recordEvents); @@ -33,16 +33,5 @@ public IScope StartScopedSpan() { return CurrentSpanUtils.WithSpan(this.StartSpan(), true); } - - // public void StartSpanAndRun(Runnable runnable) - // { - // Span span = startSpan(); - // CurrentSpanUtils.withSpan(span, /* endSpan= */ true, runnable).run(); - // } - // public final V startSpanAndCall(Callable callable) throws Exception - // { - // final Span span = startSpan(); - // return CurrentSpanUtils.withSpan(span, /* endSpan= */ true, callable).call(); - // } } } diff --git a/src/OpenCensus/Utils/Collections.cs b/src/OpenCensus/Utils/Collections.cs index 31f7db9..5889227 100644 --- a/src/OpenCensus/Utils/Collections.cs +++ b/src/OpenCensus/Utils/Collections.cs @@ -41,7 +41,7 @@ public static string ToString(IDictionary dict) return sb.ToString(); } - public static string ToString(IList list) + public static string ToString(IEnumerable list) { if (list == null) { @@ -61,11 +61,11 @@ public static string ToString(IList list) return sb.ToString(); } - public static bool AreEquivalent(IList c1, IList c2) + public static bool AreEquivalent(IEnumerable c1, IEnumerable c2) { var c1Dist = c1.Distinct(); var c2Dist = c2.Distinct(); - return c1.Count == c2.Count && c1Dist.Count() == c2Dist.Count() && c1Dist.Intersect(c2Dist).Count() == c1Dist.Count(); + return c1.Count() == c2.Count() && c1Dist.Count() == c2Dist.Count() && c1Dist.Intersect(c2Dist).Count() == c1Dist.Count(); } } } diff --git a/test/OpenCensus.Tests/Impl/Stats/MeasureMapBuilderTest.cs b/test/OpenCensus.Tests/Impl/Stats/MeasureMapBuilderTest.cs index 741bee0..00de89d 100644 --- a/test/OpenCensus.Tests/Impl/Stats/MeasureMapBuilderTest.cs +++ b/test/OpenCensus.Tests/Impl/Stats/MeasureMapBuilderTest.cs @@ -35,21 +35,21 @@ public class MeasureMapBuilderTest [Fact] public void TestPutDouble() { - IList metrics = MeasureMapBuilder.Builder().Put(M1, 44.4).Build(); + var metrics = MeasureMapBuilder.Builder().Put(M1, 44.4).Build(); AssertContains(metrics, MeasurementDouble.Create(M1, 44.4) ); } [Fact] public void TestPutLong() { - IList metrics = MeasureMapBuilder.Builder().Put(M3, 9999L).Put(M4, 8888L).Build(); + var metrics = MeasureMapBuilder.Builder().Put(M3, 9999L).Put(M4, 8888L).Build(); AssertContains(metrics, MeasurementLong.Create(M3, 9999L), MeasurementLong.Create(M4, 8888L)); } [Fact] public void TestCombination() { - IList metrics = + var metrics = MeasureMapBuilder.Builder() .Put(M1, 44.4) .Put(M2, 66.6) @@ -65,14 +65,14 @@ public void TestCombination() [Fact] public void TestBuilderEmpty() { - IList metrics = MeasureMapBuilder.Builder().Build(); + var metrics = MeasureMapBuilder.Builder().Build(); AssertContains(metrics); } [Fact] public void TestBuilder() { - IList expected = new List(10); + var expected = new List(10); MeasureMapBuilder builder = MeasureMapBuilder.Builder(); for (int i = 1; i <= 10; i++) { @@ -144,7 +144,7 @@ private static IMeasureLong MakeSimpleMeasureLong(String measure) return MeasureLong.Create(measure, measure + " description", "1"); } - private static void AssertContains(IList metrics, params IMeasurement[] measurements) + private static void AssertContains(IEnumerable metrics, params IMeasurement[] measurements) { var expected = measurements.ToList(); Assert.True(Collections.AreEquivalent(metrics, expected)); diff --git a/test/OpenCensus.Tests/Impl/Stats/StatsTestUtil.cs b/test/OpenCensus.Tests/Impl/Stats/StatsTestUtil.cs index e3e6588..b5eb8ce 100644 --- a/test/OpenCensus.Tests/Impl/Stats/StatsTestUtil.cs +++ b/test/OpenCensus.Tests/Impl/Stats/StatsTestUtil.cs @@ -18,6 +18,7 @@ namespace OpenCensus.Stats.Test { using System; using System.Collections.Generic; + using System.Linq; using OpenCensus.Common; using OpenCensus.Stats.Aggregations; using OpenCensus.Tags; @@ -142,30 +143,33 @@ private static void AssertDistributionDataEquals( Assert.Equal(RemoveTrailingZeros(expected.BucketCounts), RemoveTrailingZeros((actual).BucketCounts)); } - private static IList RemoveTrailingZeros(IList longs) + private static IEnumerable RemoveTrailingZeros(IEnumerable longs) { - if (longs == null || longs.Count == 0) + if (longs == null || longs.Any()) { return longs; } - - List truncated = new List(); - int lastIndex = longs.Count - 1; - while (longs[lastIndex] == 0) + + var buffer = new List(); + var result = new List(); + foreach (var item in longs) { - lastIndex--; - if (lastIndex <= 0) + if (item == 0) { - break; + buffer.Add(item); + } + else + { + foreach (var bufferedItem in buffer) + { + result.Add(bufferedItem); + } + buffer.Clear(); + result.Add(item); } - } - for (int i = 0; i < lastIndex; i++) - { - truncated.Add(longs[i]); } - return truncated; - + return result; } } } diff --git a/test/OpenCensus.Tests/Impl/Testing/Export/TestHandler.cs b/test/OpenCensus.Tests/Impl/Testing/Export/TestHandler.cs index 0f3b047..79c5d51 100644 --- a/test/OpenCensus.Tests/Impl/Testing/Export/TestHandler.cs +++ b/test/OpenCensus.Tests/Impl/Testing/Export/TestHandler.cs @@ -26,7 +26,7 @@ public class TestHandler : IHandler private readonly object monitor = new object(); private readonly List spanDataList = new List(); - public void Export(IList data) + public void Export(IEnumerable data) { lock (monitor) { @@ -36,9 +36,9 @@ public void Export(IList data) } - public IList WaitForExport(int numberOfSpans) + public IEnumerable WaitForExport(int numberOfSpans) { - IList ret; + IList result; lock (monitor) { while (spanDataList.Count < numberOfSpans) { @@ -56,10 +56,10 @@ public IList WaitForExport(int numberOfSpans) return new List(); } } - ret = new List(spanDataList); + result = new List(spanDataList); spanDataList.Clear(); } - return ret; + return result; } } } diff --git a/test/OpenCensus.Tests/Impl/Trace/Export/SpanExporterTest.cs b/test/OpenCensus.Tests/Impl/Trace/Export/SpanExporterTest.cs index c553b44..e96dc12 100644 --- a/test/OpenCensus.Tests/Impl/Trace/Export/SpanExporterTest.cs +++ b/test/OpenCensus.Tests/Impl/Trace/Export/SpanExporterTest.cs @@ -18,6 +18,7 @@ namespace OpenCensus.Trace.Export.Test { using System; using System.Collections.Generic; + using System.Linq; using System.Threading; using Moq; using OpenCensus.Common; @@ -89,8 +90,8 @@ public void ExportDifferentSampledSpans() { Span span1 = CreateSampledEndedSpan(SPAN_NAME_1); Span span2 = CreateSampledEndedSpan(SPAN_NAME_2); - IList exported = serviceHandler.WaitForExport(2); - Assert.Equal(2, exported.Count); + var exported = serviceHandler.WaitForExport(2); + Assert.Equal(2, exported.Count()); Assert.Contains(span1.ToSpanData(), exported); Assert.Contains(span2.ToSpanData(), exported); } @@ -104,8 +105,8 @@ public void ExportMoreSpansThanTheBufferSize() Span span4 = CreateSampledEndedSpan(SPAN_NAME_1); Span span5 = CreateSampledEndedSpan(SPAN_NAME_1); Span span6 = CreateSampledEndedSpan(SPAN_NAME_1); - IList exported = serviceHandler.WaitForExport(6); - Assert.Equal(6, exported.Count); + var exported = serviceHandler.WaitForExport(6); + Assert.Equal(6, exported.Count()); Assert.Contains(span1.ToSpanData(), exported); Assert.Contains(span2.ToSpanData(), exported); Assert.Contains(span3.ToSpanData(), exported); @@ -136,14 +137,14 @@ public void ServiceHandlerThrowsException() // .export(anyListOf(SpanData)); spanExporter.RegisterHandler("mock.service", mockServiceHandler); Span span1 = CreateSampledEndedSpan(SPAN_NAME_1); - IList exported = serviceHandler.WaitForExport(1); - Assert.Equal(1, exported.Count); + var exported = serviceHandler.WaitForExport(1); + Assert.Equal(1, exported.Count()); Assert.Contains(span1.ToSpanData(), exported); // assertThat(exported).containsExactly(span1.toSpanData()); // Continue to export after the exception was received. Span span2 = CreateSampledEndedSpan(SPAN_NAME_1); exported = serviceHandler.WaitForExport(1); - Assert.Equal(1, exported.Count); + Assert.Equal(1, exported.Count()); Assert.Contains(span2.ToSpanData(), exported); // assertThat(exported).containsExactly(span2.toSpanData()); } @@ -155,12 +156,12 @@ public void ExportSpansToMultipleServices() spanExporter.RegisterHandler("test.service2", serviceHandler2); Span span1 = CreateSampledEndedSpan(SPAN_NAME_1); Span span2 = CreateSampledEndedSpan(SPAN_NAME_2); - IList exported1 = serviceHandler.WaitForExport(2); - IList exported2 = serviceHandler2.WaitForExport(2); - Assert.Equal(2, exported1.Count); + var exported1 = serviceHandler.WaitForExport(2); + var exported2 = serviceHandler2.WaitForExport(2); + Assert.Equal(2, exported1.Count()); Assert.Contains(span1.ToSpanData(), exported1); Assert.Contains(span2.ToSpanData(), exported1); - Assert.Equal(2, exported2.Count); + Assert.Equal(2, exported2.Count()); Assert.Contains(span1.ToSpanData(), exported2); Assert.Contains(span2.ToSpanData(), exported2); } @@ -174,10 +175,10 @@ public void ExportNotSampledSpans() // sampled span is not exported by creating and ending a sampled span after a non sampled span // and checking that the first exported span is the sampled span (the non sampled did not get // exported). - IList exported = serviceHandler.WaitForExport(1); + var exported = serviceHandler.WaitForExport(1); // Need to check this because otherwise the variable span1 is unused, other option is to not // have a span1 variable. - Assert.Equal(1, exported.Count); + Assert.Equal(1, exported.Count()); Assert.DoesNotContain(span1.ToSpanData(), exported); Assert.Contains(span2.ToSpanData(), exported); diff --git a/test/OpenCensus.Tests/Impl/Trace/Sampler/SamplersTest.cs b/test/OpenCensus.Tests/Impl/Trace/Sampler/SamplersTest.cs index 2ee3ae1..887568d 100644 --- a/test/OpenCensus.Tests/Impl/Trace/Sampler/SamplersTest.cs +++ b/test/OpenCensus.Tests/Impl/Trace/Sampler/SamplersTest.cs @@ -263,7 +263,7 @@ public void ProbabilitySampler_ToString() // Applies the given sampler to NUM_SAMPLE_TRIES random traceId/spanId pairs. private static void AssertSamplerSamplesWithProbability( - ISampler sampler, ISpanContext parent, IList parentLinks, double probability) + ISampler sampler, ISpanContext parent, IEnumerable parentLinks, double probability) { RandomGenerator random = new RandomGenerator(1234); int count = 0; // Count of spans with sampling enabled