Skip to content

Commit

Permalink
Merge pull request #34 from tnunnink/hotfix/32-l5x-memory-leakage
Browse files Browse the repository at this point in the history
Hotfix/32 l5x memory leakage
  • Loading branch information
tnunnink authored Sep 24, 2024
2 parents 137dd3c + 374a149 commit eae981c
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 51 deletions.
121 changes: 83 additions & 38 deletions src/.idea/.idea.L5Sharp/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/L5Sharp.Core/L5Sharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<Title>L5Sharp</Title>
<Authors>Timothy Nunnink</Authors>
<Version>3.3.0</Version>
<AssemblyVersion>3.3.0</AssemblyVersion>
<FileVersion>3.3.0.0</FileVersion>
<Version>3.3.1</Version>
<AssemblyVersion>3.3.1</AssemblyVersion>
<FileVersion>3.3.1.0</FileVersion>
<Description>A library for intuitively interacting with Rockwell's L5X import/export files.</Description>
<RepositoryUrl>https://github.com/tnunnink/L5Sharp</RepositoryUrl>
<PackageTags>csharp allen-bradely l5x logix plc-programming rockwell-automation logix5000</PackageTags>
Expand All @@ -18,7 +18,7 @@
<!--<PackageIcon>icon.png</PackageIcon>-->
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>
Made all MESSAGE type properties nullable/no required since I found examples of L5X not having certain attributes.
Added tests to ensure no memory issues.
</PackageReleaseNotes>
<Copyright>Copyright (c) Timothy Nunnink 2022</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion src/L5Sharp.Core/L5X.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace L5Sharp.Core;
/// </summary>
/// <remarks>
/// </remarks>
public class L5X : ILogixSerializable
public sealed class L5X : ILogixSerializable
{
/// <summary>
/// The date/time format for the L5X content.
Expand Down
3 changes: 2 additions & 1 deletion src/L5Sharp.Core/LogixIndex.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

Expand Down
7 changes: 7 additions & 0 deletions src/L5Sharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "L5Sharp.Core", "L5Sharp.Cor
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "L5Sharp.Benchmarks", "..\tests\L5Sharp.Benchmarks\L5Sharp.Benchmarks.csproj", "{F52CE1E1-B8AC-431B-901F-55824D6F5E40}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "L5Sharp.Console", "..\tests\L5Sharp.Console\L5Sharp.Console.csproj", "{67153CB1-10AF-4D21-A262-CBEFB4690D41}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -32,10 +34,15 @@ Global
{F52CE1E1-B8AC-431B-901F-55824D6F5E40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F52CE1E1-B8AC-431B-901F-55824D6F5E40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F52CE1E1-B8AC-431B-901F-55824D6F5E40}.Release|Any CPU.Build.0 = Release|Any CPU
{67153CB1-10AF-4D21-A262-CBEFB4690D41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67153CB1-10AF-4D21-A262-CBEFB4690D41}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67153CB1-10AF-4D21-A262-CBEFB4690D41}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67153CB1-10AF-4D21-A262-CBEFB4690D41}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{FED5FACA-5568-4A3F-94C0-536D9E36CA26} = {337B58B2-5647-4088-8CB3-50B3752B04D8}
{EBD410F5-C4E8-4D12-86B4-08B190B50B63} = {337B58B2-5647-4088-8CB3-50B3752B04D8}
{F52CE1E1-B8AC-431B-901F-55824D6F5E40} = {337B58B2-5647-4088-8CB3-50B3752B04D8}
{67153CB1-10AF-4D21-A262-CBEFB4690D41} = {337B58B2-5647-4088-8CB3-50B3752B04D8}
EndGlobalSection
EndGlobal
16 changes: 16 additions & 0 deletions tests/L5Sharp.Console/L5Sharp.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>12</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\L5Sharp.Core\L5Sharp.Core.csproj" />
<ProjectReference Include="..\L5Sharp.Samples\L5Sharp.Samples.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions tests/L5Sharp.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using L5Sharp.Console;

while (Console.ReadLine() != "exit")
{
var test = new TestClass();
await test.RunExample();
}
23 changes: 23 additions & 0 deletions tests/L5Sharp.Console/TestClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using L5Sharp.Core;
using L5Sharp.Samples;
using Task = System.Threading.Tasks.Task;

namespace L5Sharp.Console;

public class TestClass
{
public async Task RunExample()
{
var content = await L5X.LoadAsync(Known.Example, CancellationToken.None);

var tags = content.Query<Tag>().Where(t => t.TagName.Contains("Test"));

foreach (var tag in tags)
{
if (tag.Description is not null)
{
System.Console.WriteLine($"{tag.TagName}: {tag.Description}");
}
}
}
}
1 change: 1 addition & 0 deletions tests/L5Sharp.Tests/L5Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="JetBrains.dotMemoryUnit" Version="3.2.20220510" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
Expand Down
41 changes: 34 additions & 7 deletions tests/L5Sharp.Tests/L5XBasicTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Xml.Linq;
using System.Globalization;
using System.Xml.Linq;
using FluentAssertions;
using JetBrains.dotMemoryUnit;

namespace L5Sharp.Tests;

Expand Down Expand Up @@ -70,7 +72,7 @@ public void New_NullProcessor_ShouldThrowException()
{
FluentActions.Invoking(() => L5X.New("Test", null!, new Revision())).Should().Throw<ArgumentException>();
}

[Test]
public void New_NullRevision_ShouldThrowException()
{
Expand Down Expand Up @@ -183,7 +185,7 @@ public void All_ValidTypeAndNullName_ShouldThrowException()

FluentActions.Invoking(() => content.All<Tag>(null!)).Should().Throw<ArgumentException>();
}

[Test]
public void All_ValidTypeAndEmptyName_ShouldThrowException()
{
Expand Down Expand Up @@ -230,18 +232,18 @@ public void Find_ValidKey_ShouldBeExpectedTypeAndName()
var content = L5X.Load(Known.Test);

var component = content.Find(new ComponentKey("DataType", Known.DataType));

component.Should().NotBeNull();
component?.Name.Should().Be(Known.DataType);
}

[Test]
public void Find_ValidKeyAndContainer_ShouldBeExpectedTypeAndName()
{
var content = L5X.Load(Known.Test);

var component = content.Find(new ComponentKey("Tag", Known.Tag));

component.Should().NotBeNull();
component?.Name.Should().Be(Known.Tag);
}
Expand All @@ -256,7 +258,7 @@ public void Find_ValidComponent_ShouldBeExpectedName()
component.Should().NotBeNull();
component?.Name.Should().Be(Known.DataType);
}

[Test]
public void Find_ValidTagName_ShouldNotBeNull()
{
Expand Down Expand Up @@ -324,4 +326,29 @@ public Task Serialize_WhenCalled_ShouldBeValid()
.ScrubMember("ProjectCreationDate")
.ScrubMember("LastModifiedDate");
}

[DotMemoryUnit(FailIfRunWithoutSupport = false)]
[Test]
public void CheckForMemoryLeaksTest()
{
var isolator = new Action(() =>
{
// ReSharper disable once RedundantAssignment
var content = L5X.Load(Known.Example);
var tags = content.Query<Tag>().Where(t => t.TagName.Contains("Test"));
tags.Should().NotBeEmpty();
content = null;
content.Should().BeNull();
});

isolator();

GC.Collect();
GC.WaitForFullGCComplete();

// Assert L5X is removed from memory
dotMemory.Check(memory => memory.GetObjects(where => where.Type.Is<L5X>()).ObjectsCount.Should().Be(0));
}
}

0 comments on commit eae981c

Please sign in to comment.