Skip to content

Commit

Permalink
Unify the recorder side of the process
Browse files Browse the repository at this point in the history
  -- make the IPC mechanism available for all instrumented code
  • Loading branch information
SteveGilham committed Jan 30, 2018
1 parent 6ae5dd3 commit 3aafbc7
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 125 deletions.
2 changes: 1 addition & 1 deletion AltCover.Recorder/AltCover.Recorder.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<Link>VisibleToTest.fs</Link>
</Compile>
<Compile Include="Base.fs" />
<Compile Include="Framework.fs" />
<Compile Include="Tracer.fs" />
<Compile Include="Recorder.fs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion AltCover.Recorder/AltCover.Shadow.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<Link>AssemblyVersion.fs</Link>
</Compile>
<Compile Include="Base.fs" />
<Compile Include="Framework.fs" />
<Compile Include="Tracer.fs" />
<Compile Include="Recorder.fs" />
</ItemGroup>
<ItemGroup>
Expand Down
24 changes: 0 additions & 24 deletions AltCover.Recorder/Framework.fs

This file was deleted.

2 changes: 1 addition & 1 deletion AltCover.Recorder/Recorder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ module Instance =
do
AppDomain.CurrentDomain.DomainUnload.Add(FlushCounter false)
AppDomain.CurrentDomain.ProcessExit.Add(FlushCounter true)
trace.OnStart ()
trace <- trace.OnStart ()
19 changes: 12 additions & 7 deletions AltCover.Recorder/netcore.fs → AltCover.Recorder/Tracer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,40 @@ open System.IO

type Tracer = {
Tracer : string
Stream : System.IO.FileStream ref
Stream : System.IO.FileStream
Formatter : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
}
with
#if NETSTANDARD2_0
static member Core () =
typeof<Microsoft.FSharp.Core.CompilationMappingAttribute>.Assembly.Location
#endif

static member Create (name:string) =
{
Tracer = name
Stream = ref null
Stream = null
Formatter = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
}

member this.IsConnected () =
match !this.Stream with
match this.Stream with
| null -> false
| _ -> File.Exists this.Tracer

member this.Connect () =
if File.Exists this.Tracer then
this.Stream := File.OpenWrite(this.Tracer)
{ this with Stream = File.OpenWrite(this.Tracer) }
else
this

member this.Close() =
match !this.Stream with
match this.Stream with
| null -> ()
| _ -> (!this.Stream).Dispose()
| _ -> this.Stream.Dispose()

member this.Push (moduleId:string) hitPointId =
let stream = !this.Stream
let stream = this.Stream
this.Formatter.Serialize(stream, (moduleId, hitPointId))
stream.Flush()

Expand All @@ -50,6 +54,7 @@ type Tracer = {
member this.OnStart () =
if this.Tracer <> "Coverage.Default.xml.bin" then
this.Connect ()
else this

member this.OnConnected f l g =
if this.IsConnected() then f()
Expand Down
3 changes: 1 addition & 2 deletions AltCover.Recorder/altcover.recorder.core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
<Compile Include="..\_Generated\VisibleToTest.fs" Link="VisibleToTest.fs" />
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Base.fs" />
<None Include="Framework.fs" />
<Compile Include="netcore.fs" />
<Compile Include="Tracer.fs" />
<Compile Include="Recorder.fs" />
</ItemGroup>

Expand Down
64 changes: 0 additions & 64 deletions Shadow.Tests/Framework.fs

This file was deleted.

11 changes: 5 additions & 6 deletions Shadow.Tests/Shadow.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ type AltCoverTests() = class

[<Test>]
member self.ShouldBeLinkingTheCorrectCopyOfThisCode() =
let locker = { Tracer = String.Empty
#if NETCOREAPP2_0
Stream = ref null
Formatter = null
#endif
let tracer = {
Tracer = String.Empty
Stream = null
Formatter = null
}
Assert.That(locker.GetType().Assembly.GetName().Name, Is.EqualTo
Assert.That(tracer.GetType().Assembly.GetName().Name, Is.EqualTo
#if NETCOREAPP2_0
"AltCover.Recorder")
#else
Expand Down
2 changes: 1 addition & 1 deletion Shadow.Tests/Shadow.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<Compile Include="..\_Generated\AssemblyVersion.fs">
<Link>AssemblyVersion.fs</Link>
</Compile>
<Compile Include="Framework.fs" />
<Compile Include="Tracer.fs" />
<Compile Include="Shadow.Tests.fs" />
<Content Include="packages.config" />
<EmbeddedResource Include="SimpleCoverage.xml" />
Expand Down
63 changes: 48 additions & 15 deletions Shadow.Tests/netcore.fs → Shadow.Tests/Tracer.fs
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
namespace Tests.Shadow.Core
#if NETCOREAPP2_0
namespace Tests.Shadow.Core
#else
#if NET4
namespace Tests.Shadow.Clr4
#else
#if NET2
namespace Tests.Shadow.Clr2
#else
#if MONO
namespace Tests.Shadow.Mono
#else
namespace Tests.Shadow.Unknown
#endif
#endif
#endif
#endif

open System
open System.Collections.Generic
open System.IO
open System.Reflection
open System.Threading
open System.Threading.Tasks

open AltCover.Recorder
open NUnit.Framework

[<TestFixture>]
type AltCoverCoreTests() = class

[<Test>]
member self.WillNotConnectSpontaneously () =
let where = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
let unique = Path.Combine(where, Guid.NewGuid().ToString())
let mutable client = Tracer.Create unique
try
client <- client.OnStart()
Assert.That (client.IsConnected(), Is.False)
finally
client.Close()

[<Test>]
member self.ValidTokenWillConnect () =
let where = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
Expand All @@ -21,9 +46,9 @@ type AltCoverCoreTests() = class
use stream = File.Create(unique)
()

let client = Tracer.Create unique
let mutable client = Tracer.Create unique
try
client.OnStart()
client <- client.OnStart()
Assert.That (client.IsConnected(), Is.True)
finally
client.Close()
Expand All @@ -46,21 +71,24 @@ type AltCoverCoreTests() = class
let save = Instance.trace
let where = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
let unique = Path.Combine(where, Guid.NewGuid().ToString())
let expected = [("name", 23)]
let expected = [("name", 23); ("name", 23)]

do
use stream = File.Create unique
()
try
let client = Tracer.Create unique
let mutable client = Tracer.Create unique
try
Instance.Visits.Clear()
client.OnStart()
Instance.trace <- client
let entry = Dictionary<int, int>()
entry.Add(23, 1)
Instance.Visits.Add("name", entry)

Instance.trace <- client.OnStart()
Assert.That (Instance.trace.IsConnected(), "connection failed")
Instance.Visit "name" 23
finally
client.Close()
Instance.trace.Close()
Instance.trace <- save

use stream = File.OpenRead unique
Expand All @@ -73,25 +101,28 @@ type AltCoverCoreTests() = class


[<Test>]
member self.FlushShouldTidyUp() =
member self.FlushShouldTidyUp() = // also throw a bone to OpenCover 615
let save = Instance.trace
let where = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
let unique = Path.Combine(where, Guid.NewGuid().ToString())
let expected = [("name", 23); (null, -1)]

do
use stream = File.Create unique
()

try
let client = Tracer.Create unique
let expected = [("name", client.GetHashCode()); (null, -1)]

try
Instance.Visits.Clear()
client.OnStart()
Instance.trace <- client
Instance.trace <- client.OnStart()
Assert.That(Instance.trace.Equals client, Is.False)
Assert.That(Instance.trace.Equals expected, Is.False)

Assert.That (Instance.trace.IsConnected(), "connection failed")
let formatter = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
formatter.Serialize(!Instance.trace.Stream, expected |> Seq.head)
formatter.Serialize(Instance.trace.Stream, expected |> Seq.head)
Instance.FlushCounter true ()
finally
client.Close()
Expand All @@ -105,9 +136,11 @@ type AltCoverCoreTests() = class
finally
Instance.Visits.Clear()

#if NETCOREAPP2_0
[<Test>]
member self.CoreFindsThePlace() =
Assert.That (AltCover.Recorder.Tracer.Core(),
Does.EndWith("FSharp.Core.dll"))
#endif

end
2 changes: 1 addition & 1 deletion Shadow.Tests/altcover.recorder.tests.core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="netcore.fs" />
<Compile Include="Tracer.fs" />
<Compile Include="Shadow.Tests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Shadow.Tests2/Shadow.Tests2.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
<Compile Include="..\_Generated\VisibleToTest.fs">
<Link>VisibleToTest.fs</Link>
</Compile>
<Compile Include="..\Shadow.Tests\Framework.fs">
<Link>Framework.fs</Link>
<Compile Include="..\Shadow.Tests\Tracer.fs">
<Link>Tracer.fs</Link>
</Compile>
<Compile Include="..\Shadow.Tests\Shadow.Tests.fs">
<Link>Shadow.Tests.fs</Link>
Expand Down

0 comments on commit 3aafbc7

Please sign in to comment.