Commit 27603c46 authored by Martin Finkel's avatar Martin Finkel Committed by Martin Finkel

Happy path seems to work fine, but need figure out how to test for timeout and token cancellation

parent 5cf1c137
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
......@@ -64,6 +65,9 @@ namespace LibVLCSharp.Tests
Assert.True(media.IsParsed);
Assert.NotZero(media.Duration);
using (var mp = new MediaPlayer(media))
/// <summary>
/// add media file for tests in \bin\x64\Debug\net47
/// </summary>
{
Assert.True(mp.Play());
await Task.Delay(3000); // have to wait a bit for statistics to populate
......@@ -73,7 +77,7 @@ namespace LibVLCSharp.Tests
}
}
}
[Test]
public void Duplicate()
{
......@@ -103,12 +107,47 @@ namespace LibVLCSharp.Tests
}
[Test]
public void GetTracks()
public async Task AsyncParse()
{
var media = new Media(new Instance(), RealMp3Path);
media.Parse();
var tracks = media.Tracks;
var media = new Media(new Instance(), RealMediaPath, Media.FromType.FromPath);
var result = await media.ParseAsyncWithOptions();
Assert.True(result);
}
[Test]
public async Task AsyncParseTimeoutStop()
{
//TODO: fix
Assert.Inconclusive();
var media = new Media(new Instance(), RealMediaPath, Media.FromType.FromPath);
var called = false;
media.EventManager.ParsedChanged += (sender, args) =>
{
Assert.True(args.ParsedStatus == Media.MediaParsedStatus.Timeout);
called = true;
};
var result = await media.ParseAsyncWithOptions(timeout: 1);
Assert.False(result);
Assert.True(called);
}
[Test]
public async Task AsyncParseCancel()
{
//TODO: fix
Assert.Inconclusive();
var media = new Media(new Instance(), RealMediaPath, Media.FromType.FromPath);
var called = false;
media.EventManager.ParsedChanged += (sender, args) =>
{
Assert.True(args.ParsedStatus == Media.MediaParsedStatus.Failed);
called = true;
};
// current code cancels tasks before the parsing even starts so parseStatus is never set to failed.
var result = await media.ParseAsyncWithOptions(cancellationToken: new CancellationToken(canceled: true));
Assert.False(result);
Assert.True(called);
}
}
}
......@@ -601,39 +601,53 @@ namespace LibVLCSharp.Shared
}
TaskCompletionSource<bool> _tcs;
Task<bool> ParseAsyncWithOptionsInternal(Func<bool> nativeCall, CancellationToken cancellationToken, int timeout)
async Task<bool> ParseAsyncWithOptionsInternal(Func<bool> nativeCall, CancellationToken cancellationToken, int timeout)
{
_tcs = new TaskCompletionSource<bool>();
var timeoutToken = new CancellationTokenSource(timeout);
try
{
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();
EventManager.ParsedChanged += OnParsedChanged;
_tcs = new TaskCompletionSource<bool>();
var timeoutToken = new CancellationTokenSource(timeout);
cancellationToken.Register(() =>
{
Native.LibVLCMediaParseStop(NativeReference);
_tcs.SetCanceled();
});
EventManager.ParsedChanged += OnParsedChanged;
timeoutToken.Token.Register(() =>
{
Native.LibVLCMediaParseStop(NativeReference);
_tcs.SetCanceled();
});
cancellationToken.Register(() =>
{
EventManager.ParsedChanged -= OnParsedChanged;
Native.LibVLCMediaParseStop(NativeReference);
_tcs?.TrySetCanceled();
});
timeoutToken.Token.Register(() =>
{
EventManager.ParsedChanged -= OnParsedChanged;
Native.LibVLCMediaParseStop(NativeReference);
_tcs?.TrySetCanceled();
});
try
{
var result = nativeCall();
// TODO: asynchronously wait until EventManager.ParsedChanged or timeout hits
}
catch (OperationCanceledException)
{
_tcs?.TrySetCanceled();
return false;
}
catch (Exception ex)
{
_tcs.SetException(ex);
_tcs?.TrySetException(ex);
return false;
}
finally
{
}
return _tcs.Task;
//EventManager.ParsedChanged -= OnParsedChanged;
if (_tcs == null)
return false;
return await _tcs.Task;
}
void OnParsedChanged(object sender, MediaParsedChangedEventArgs mediaParsedChangedEventArgs)
......@@ -642,7 +656,8 @@ namespace LibVLCSharp.Shared
_tcs?.TrySetResult(true);
else if (ParsedStatus == MediaParsedStatus.Failed)
_tcs?.TrySetException(new VLCException($"parsing of {this} failed"));
else _tcs?.SetResult(false);
else _tcs?.TrySetResult(false);
EventManager.ParsedChanged -= OnParsedChanged;
}
/// <summary>Get Parsed status for media descriptor object.</summary>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment