Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧Fix issue with failing tests #385

Closed
11 tasks done
CalvinWilkinson opened this issue Jan 31, 2025 · 0 comments · Fixed by #386
Closed
11 tasks done

🚧Fix issue with failing tests #385

CalvinWilkinson opened this issue Jan 31, 2025 · 0 comments · Fixed by #386
Assignees
Labels
🐛bug Something isn't working high-priority High Priority preview Done while in preview

Comments

@CalvinWilkinson
Copy link
Member

CalvinWilkinson commented Jan 31, 2025

Complete The Item Below

  • I have updated the title without removing the 🚧 emoji.

Description

Fix an issue with failing tests where the tests fail on macOS Sequoia version 15.1.1.

After some disccusion back and forth and some testing myself on Windows, we discovered that the tests fail or pass in the following ways:

  1. macOS with dotnet test command - Fail
  2. Windows with dotnet test command - Fail
  3. Windows using Rider - Pass

This was reported by Bonda (Velaptor contributor), that the test was failing when he was running tests on .
He reported that following exception:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at CASL.Devices.AudioDeviceManager.DestroyDevice() in /Users/andrea/repo/personal/KinsonDigital/CASL/CASL/Devices/AudioDeviceManager.cs:line 169
   at CASL.Devices.AudioDeviceManager.Dispose(Boolean disposing) in /Users/andrea/repo/personal/KinsonDigital/CASL/CASL/Devices/AudioDeviceManager.cs:line 146
   at CASL.Devices.AudioDeviceManager.Finalize() in /Users/andrea/repo/personal/KinsonDigital/CASL/CASL/Devices/AudioDeviceManager.cs:line 45
The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at CASL.Devices.AudioDeviceManager.DestroyDevice() in /Users/andrea/repo/personal/KinsonDigital/CASL/CASL/Devices/AudioDeviceManager.cs:line 169
   at CASL.Devices.AudioDeviceManager.Dispose(Boolean disposing) in /Users/andrea/repo/personal/KinsonDigital/CASL/CASL/Devices/AudioDeviceManager.cs:line 146
   at CASL.Devices.AudioDeviceManager.Finalize() in /Users/andrea/repo/personal/KinsonDigital/CASL/CASL/Devices/AudioDeviceManager.cs:line 45

We discovered that the failure was coming from the following method:

private void DestroyDevice()
{
    this.alInvoker.MakeContextCurrent(ALContext.Null());
    this.alInvoker.DestroyContext(this.context);
    this.context = ALContext.Null();

    this.alInvoker.CloseDevice(new ALDevice(this.device));
    this.device = ALDevice.Null();

    this.attributes = null;
}

I also discovered that the failure was occurring with the following types:

  1. AudioDeviceManager
  2. FullBuffer
  3. Audio
  4. StreamBuffer

Further investigation revealed that the reason for the failure was the timing and execution of finalizer methods depending on which OS and method of running tests.

To solve this issue, add the following static class to detect if the finalizers are being invoked from a unit test.

Details

// <copyright file="UnitTestDetector.cs" company="KinsonDigital">
// Copyright (c) KinsonDigital. All rights reserved.
// </copyright>

namespace CASL;

using System;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// Detects if the code ahs been executed from a unit test vs the rest of the application.
/// </summary>
[ExcludeFromCodeCoverage(Justification = "Do not need to see coverage for code used for testing.")]
internal static class UnitTestDetector
{
    private static bool isRunningFromUnitTest;
    
    /// <summary>
    /// Initializes static members of the <see cref="UnitTestDetector"/> class.
    /// </summary>
    static UnitTestDetector()
    {
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();

        foreach (var assembly in assemblies)
        {
            if (string.IsNullOrEmpty(assembly.FullName) ||
                !assembly.FullName.ToLowerInvariant().StartsWith("xunit."))
            {
                continue;
            }

            isRunningFromUnitTest = true;
            break;
        }
    }

    /// <summary>
    /// Gets a value indicating whether the code is being executed from a unit test.
    /// </summary>
    public static bool IsRunningFromUnitTest
    {
        get
        {
#if DEBUG
            return isRunningFromUnitTest;
#else
            return false;
#endif
        }
    }
}

Acceptance Criteria

The items to complete to satisfy the Definition of Done.

Preview Give feedback

ToDo Items

The items to complete to satisfy the Definition of Done.

Preview Give feedback

Issue Dependencies

No response

Related Work

No response

Additional Information:

Unit Tests

Reasons for local unit test execution:

  • Unit tests might pass locally but not in the CI environment during the status check process or vice-versa.
  • Tests might pass on the developer's machine but not necessarily on the code reviewer's machine.
  • If you notice that the test status check has passed but the test failed locally, please notify a project maintainer!

💡Warning💡
If the unit tests pass remotely and are not executed locally, this means we could be letting a bug slip into production.
Though bugs will always exist in some capacity, we should all do our part to help prevent them from happening.

Change Type Labels

Change Type Label
Bug Fixes 🐛bug
Breaking Changes 🧨breaking-changes
New Feature ✨new-feature
CICD Changes ♻️cicd
Config Changes ⚙️config
Performance Improvements 🏎️performance
Code Doc Changes 🗒️documentation/code
Product Doc Changes 📝documentation/product

Priority Type Labels

Priority Type Label
Low Priority low priority
Medium Priority medium priority
High Priority high priority

Code of Conduct

  • I agree to follow this project's Code of Conduct.
@CalvinWilkinson CalvinWilkinson added high-priority High Priority preview Done while in preview 🐛bug Something isn't working labels Jan 31, 2025
@CalvinWilkinson CalvinWilkinson added this to the v1.0.0-preview.21 milestone Jan 31, 2025
@CalvinWilkinson CalvinWilkinson self-assigned this Jan 31, 2025
@github-project-automation github-project-automation bot moved this to ⚪Not Set in KD-Team Jan 31, 2025
@CalvinWilkinson CalvinWilkinson moved this from ⚪Not Set to 🏗️In Development in KD-Team Jan 31, 2025
CalvinWilkinson added a commit that referenced this issue Jan 31, 2025
CalvinWilkinson added a commit that referenced this issue Feb 3, 2025
* Start work for issue #385

* fix: create class to detect if the code is running from a unit test

* fix: add the unit test detector check to all the finalizers
@github-project-automation github-project-automation bot moved this from 🏗️In Development to ✅Done in KD-Team Feb 3, 2025
CalvinWilkinson added a commit that referenced this issue Feb 3, 2025
* Start work for issue #385

* fix: create class to detect if the code is running from a unit test

* fix: add the unit test detector check to all the finalizers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛bug Something isn't working high-priority High Priority preview Done while in preview
Projects
Status: ✅Done
Development

Successfully merging a pull request may close this issue.

1 participant