c# - 使用 ManualResetEvent 锁定测试对 NAudio 进行单元测试

标签 c# .net unit-testing naudio

好吧,我正在尝试针对我为录音 session 创建的包装器对 NAudio 进行单元测试,这是启动和停止录音 session 的代码......

public void StartRecording(string claimNo, string ip_no, string ip_name)
{
    if (this.IsRecording)
    {
        return;
    }

    this.Recordings.Add(new RecordingTrack(claimNo, ip_no, ip_name));
    if (this.MicrophoneLevel == default(float))
    {
        this.MicrophoneLevel = .75f;
    }

    _aggregator.Reset();

    _input = new WaveIn();
    _input.WaveFormat = _waveFormat;

    _input.DataAvailable += (s, args) =>
        {
            _writer.Write(args.Buffer, 0, args.BytesRecorded);
            byte[] buffer = args.Buffer;
            for (int index = 0; index < args.BytesRecorded; index += 2)
            {
                short sample = (short)((buffer[index + 1] << 8) | buffer[index + 0]);
                float sample32 = sample / 32768f;
                _aggregator.Add(sample32);
            }

            if (this.DataAvailable != null)
            {
                this.DataAvailable(s, args);
            }

            if (!this.IsRecording)
            {
                _writer.Close();
                _writer.Dispose();
                _writer = null;
            }
        };
    _input.RecordingStopped += (s, args) =>
        {
            _input.Dispose();
            _input = null;

            if (this.RecordingStopped != null)
            {
                this.RecordingStopped(s, args);
            }
        };

    _writer = new WaveFileWriter(this.CurrentRecording.FileName, _input.WaveFormat);

    _input.StartRecording();
    this.IsRecording = true;
}

public void StopRecording()
{
    if (!this.IsRecording)
    {
        return;
    }

    this.CurrentRecording.Stop();

    this.IsRecording = false;
    _input.StopRecording();
}

...下面是我的单元测试。我正在使用 ManualResetEvent 断言事件被触发的成功,它是这样声明的......

private ManualResetEvent _eventRaised = new ManualResetEvent(false);

...然而,问题是下面的测试只是简单地锁定并且事件永远不会被触发。 您能否确认问题是 WaitOne 不允许事件触发,因为它锁定了同一个线程?

bool success = false;
_eventRaised.Reset();

var target = new RecordingSession();
target.StartRecording("1", "01", "Test Name");

target.RecordingStopped += (s, args) =>
    {
        success = (target.CurrentRecording.Duration.TotalSeconds > 4);
        _eventRaised.Set();
    };

Thread.Sleep(4000);
target.StopRecording();

_eventRaised.WaitOne();

Assert.IsTrue(success);

如果是这样,你能帮我做这个测试吗?我需要一些启发。

我已经多次使用 ManualResetEvent 来测试其他类上的事件并且它有效,但这里有些不同。

最佳答案

您永远不会收到事件,因为 WaveIn 的默认构造函数使用窗口回调,并且您没有在 GUI 线程上运行单元测试。您应该使用 WaveInEvent 而不是在非 gui 线程上工作。在最新的 NAudio 代码中,应抛出 InvalidOperationException 以防止您犯此错误。

关于c# - 使用 ManualResetEvent 锁定测试对 NAudio 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12780411/

相关文章:

unit-testing - 从批处理文件设置错误级别

c# - Visual Studio的 "Add Service Reference"是如何反序列化对象的?

c# - 究竟应该如何使用 IAppBuilder.CreatePerOwinContext<T>?

c# - 如何将从套接字接收到的字节数组转换为 C# 结构

c# - 在 Entity Framework 中调用 AsNoTracking 的位置是否重要

c# - .NET WCF 命名管道与 COM 对象性能

.net - 无法转换类型为 'WhereArrayIterator` 1[System.String ]' to type ' System.String[]' 的对象

c# - 如何判断哪个 TestCaseData 失败(在 TestCaseSource 代码中)

unit-testing - 使用 jest 测试 nuxt.js 应用程序时访问 `process.env` 属性

c# - 寻找一本关于如何在 .net c# 中更有效地编码的好书