Async task within MvvmCross Command not returning - c#

I am building a small project using MvvMCross within a Xamarin PCL project and having issue with an async Task that I am calling within a command that is bound to a button.
I have a fake web service where-in I simply call Task.Delay(3000). When the process gets to this point it simply sits and does nothing.
I originally had the command call using the .wait() call but read somewhere that this was a blocking call and cant be miced with "async / wait"
Could someone help and possible give me a hint as to where I am going wrong on the command binding please ?
https://bitbucket.org/johncogan/exevaxamarinapp is the public git repo, the specific command is
public ICommand SaveProfile
within the ProfileViewModel.cs file.
The specific code is:
public ICommand SaveProfile
{
get
{
return new MvxCommand(() =>
{
if (_profile.IsValidData())
{
// Wait for task to compelte, do UI updates here
// TODO Throbber / Spinner
EnumWebServiceResult taskResult;
Mvx.Resolve<IProfileWebService>().SendProfileToServer(_profile).Wait();
if(_profileWebService.getLastResponseResult() == true){
taskResult = EnumWebServiceResult.SUCCESS;
}else{
taskResult = EnumWebServiceResult.FAILED_UNKNOWN;
}
//_profileWebService.SendProfileToServer(_profile).Wait();
// Close(this);
}
});
}
}
The web service class () is:
using System;
using System.Threading.Tasks;
using ExevaXamarinApp.Models;
namespace ExevaXamarinApp.Services
{
public class FakeProfileWebService : IProfileWebService
{
public int _delayPeriod { get; private set; }
public bool? lastResult;
/// <summary>
/// Initializes a new instance of the <see cref="T:ExevaXamarinApp.Enumerations.FakeProfileWebService"/> class.
/// </summary>
/// 3 second delay to simulate a remote request
public FakeProfileWebService()
{
_delayPeriod = 3000;
lastResult = null;
}
private Task Sleep()
{
return Task.Delay(3000);
}
public bool? getLastResponseResult(){
return lastResult;
}
/// <summary>
/// Sends the profile to server asynchronously
/// </summary>
/// <returns>EnumWebServiceResultFlag value</returns>
/// <param name="profileObject">Profile model object</param>
public async Task SendProfileToServer(Profile profileObject)
{
// Validate arguments before attempting to use web serivce
if (profileObject.IsValidData())
{
// TODO: Return ENUM FLAG that represents the state of the result
await Sleep();
lastResult = true;
}else{
lastResult = false;
}
}
}
}

Please try this:
public ICommand SaveProfile
{
get
{
return new MvxCommand(async () => // async added
{
if (_profile.IsValidData())
{
// Wait for task to compelte, do UI updates here
// TODO Throbber / Spinner
EnumWebServiceResult taskResult;
await Mvx.Resolve<IProfileWebService>().SendProfileToServer(_profile).ConfigureAwait(false); // await, confi.. added
if(_profileWebService.getLastResponseResult() == true){
taskResult = EnumWebServiceResult.SUCCESS;
}else{
taskResult = EnumWebServiceResult.FAILED_UNKNOWN;
}
//_profileWebService.SendProfileToServer(_profile).Wait();
// Close(this);
}
});
}
}
private async Task Sleep() // async added
{
return await Task.Delay(3000).ConfigureAwait(false); // await, confi... added
}
public async Task SendProfileToServer(Profile profileObject)
{
// Validate arguments before attempting to use web serivce
if (profileObject.IsValidData())
{
// TODO: Return ENUM FLAG that represents the state of the result
await Sleep().ConfigureAwait(false); // await, confi... added
lastResult = true;
}else{
lastResult = false;
}
}
The problem is, that the context from the UI and the async cause a deadlock.

Related

What causes this AndroidRuntimeException?

I need some help to understand why this exception is thrown. The exception is:
Android.Util.AndroidRuntimeException: Only the original thread that created a view hierarchy can touch its views.
Link to hastebin with full exception
I use ZXing.Net.Mobile.Forms for barcode scanning and Rg.Plugins.Popup for showing a popup. I believe one of them causes the exception.
The exception seems to be thrown at random. The app works fine 99 % of the time.
ScannerPage.xaml
<zxing:ZXingScannerView x:Name="ScannerView"
Result="{Binding ScanResult, Mode=OneWayToSource}"
ScanResultCommand="{Binding ScanResultCommand}"
IsScanning="{Binding IsScanning}"
IsAnalyzing="{Binding IsAnalyzing}" />
<zxing:ZXingDefaultOverlay x:Name="ScannerOverlay"
BottomText="Scanning will happen automatically"
ShowFlashButton="False"/>
ScannerPageViewModel.cs (stripped of irrelevant parts)
[PropertyChanged.AddINotifyPropertyChangedInterface]
internal class ScannerPageViewModel : INavigatedAware
{
public ScannerPageViewModel(IScannerService scannerService, IUserDialogs dialogs, IPopupNavigation popups, IScreenService screen)
{
ScanResultCommand = new Command(ProcessBarcode);
}
public ICommand ScanResultCommand { get; }
/// <summary>
/// Show info dialog box with ticket info.
/// </summary>
private async Task ShowInfoScanResult(string message)
{
var popup = new PopupViews.InfoScanResult(Popups, message);
popup.Disappearing += (se, ev) => IsAnalyzing = true;
await Popups.PushAsync(popup);
}
private void ProcessBarcode()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (ScanResult != null && !string.IsNullOrEmpty(ScanResult.Text))
{
// Disable the scanner after one barcode is found.
IsAnalyzing = false;
var source = new CancellationTokenSource();
// Show loading animation if scanning takes >1 second.
var t = Task.Run(async () =>
{
await Task.Delay(1000, source.Token);
Device.BeginInvokeOnMainThread(ShowLoading);
});
// Call the web service to process the barcode.
var scanResponse = await ScannerService.ScanBarcode(ScanResult.Text, ScanningSession, SelectedScanAction);
if (scanResponse.IsSuccessful)
{
var scanResult = scanResponse.Data;
if (scanResult.Success)
{
var json = scanResult.BarcodeInfo;
var message = ParseJsonBarcodeInfo(json);
if (SelectedScanAction == ScanAction.Information)
await ShowInfoScanResult(message);
else
await ShowOkScanResult(message);
}
else
{
await ShowErrorScanResult(scanResult.FaultDescription);
}
}
else
{
ShowScanRequestError(scanResponse.ErrorMessage);
}
source.Cancel(); // Cancel loading animation timer.
HideLoading();
Screen.SetFullscreen();
source.Dispose();
}
});
}
I have created the Dependency service for Android for me it's working perfect check a below code.
PCL Project
public interface IBarcodeScanner
{
Task<string> ScanAsync();
}
And then in Android project
[assembly: Dependency(typeof(BarcodeScanner))]
namespace CodeMashScanner.Droid.Helpers
{
public class BarcodeScanner : IBarcodeScanner
{
public async Task<string> ScanAsync()
{
var scanner = new ZXing.Mobile.MobileBarcodeScanner(Forms.Context;
var scanResults = await scanner.Scan();
return scanResults.Text;
}
}
}

async method does not wait till the user action is completed

I have a static class with a async method in it . The async method awaits a user action (user needs to confirms on a dialog). I need to call this method in a constructor in App.xaml.cs (xamarin)
public static class StoragePermission
{
public static async Task Check()
{
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
if (status != PermissionStatus.Granted)
{
var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Storage });
status = results[Permission.Storage];
}
}
catch (Exception ex) {}
}
}
And I have tried calling this method in the constructor like this:
StoragePermission.Check().Wait();
But doing the above , I dont get the next code to invoke at all. After the user action, focus does not return to the code.
I have also tried it this way:
public Task StorageInitialization { get; private set; }
and then within constructor, I do
StorageInitialization = StoragePermission.Check();
But doing this code returns to the next line in constructor, but does not wait for the user action to be completed.
I appreciate if someone could help me with this

How to Unit Test a void method with a Task inside

I have a graphic method CancelChanges() used and called by a ViewModel.
I want to test this method but we have a Task inside.
We use a Task to not freeze the UI.
My test method needs to wait the result of this Task to check the result.
The code is:
public override void CancelChanges()
{
Task.Run(
async () =>
{
this.SelectedWorkflow = null;
AsyncResult<IncidentTypeModel> asyncResult = await this.Dataprovider.GetIncidentTypeByIdAsync(this.Incident.Id);
Utils.GetDispatcher().Invoke(
() =>
{
if (asyncResult.IsError)
{
WorkflowMessageBox.ShowException(
MessageHelper.ManageException(asyncResult.Exception));
}
else
{
this.Incident = asyncResult.Result;
this.Refreshdesigner();
this.HaveChanges = false;
}
});
});
}
And my test method:
/// <summary>
/// A test for CancelChanges
/// </summary>
[TestMethod]
[TestCategory("ConfigTool")]
public void CancelChangesTest()
{
string storexaml = this._target.Incident.WorkflowXamlString;
this._target.Incident.WorkflowXamlString = "dsdfsdgfdsgdfgfd";
this._target.CancelChanges();
Assert.IsTrue(storexaml == this._target.Incident.WorkflowXamlString);
Assert.IsFalse(this._target.HaveChanges);
}
How can we do to have my test that is waiting the result of the Task?
Thanks.
Make the CancelChanges method return a Task, and then await this or set up a continuation in the test method. Some this like
public override Task CancelChanges()
{
return Task.Factory.StartNew(() =>
{
// Do stuff...
});
}
notice the change from Task.Run to Task.Factory.StartNew. This is a better way of starting tasks in such cases. Then in the test method
[TestMethod]
[TestCategory("ConfigTool")]
public void CancelChangesTest()
{
string storexaml = this._target.Incident.WorkflowXamlString;
this._target.Incident.WorkflowXamlString = "dsdfsdgfdsgdfgfd";
this._target.CancelChanges().ContinueWith(ant =>
{
Assert.IsTrue(storexaml == this._target.Incident.WorkflowXamlString);
Assert.IsFalse(this._target.HaveChanges);
});
}
You could also mark the test method as async and use await in the test method to do the same thing.
I hope this helps.
I would take the refactoring one step further, and if possible avoid using Task.Run. Since all you do is await and then invoke work on the UI Thread, I would do the following:
public override Task CancelChanges()
{
this.SelectedWorkflow = null;
AsyncResult<IncidentTypeModel> asyncResult = await this.Dataprovider.GetIncidentTypeByIdAsync(this.Incident.Id);
if (asyncResult.IsError)
{ WorkflowMessageBox.ShowException(MessageHelper.ManageException(asyncResult.Exception));
}
else
{
this.Incident = asyncResult.Result;
this.Refreshdesigner();
this.HaveChanges = false;
}
});
});
}
And the test method:
[TestMethod]
[TestCategory("ConfigTool")]
public async Task CancelChangesTest()
{
string storexaml = this._target.Incident.WorkflowXamlString;
this._target.Incident.WorkflowXamlString = "dsdfsdgfdsgdfgfd";
var cancelChanges = await this._target.CancelChanges();
Assert.IsTrue(storexaml == this._target.Incident.WorkflowXamlString);
Assert.IsFalse(this._target.HaveChanges);
}

Simulate a delay in execution in Unit Test using Moq

I'm trying to test the following:
protected IHealthStatus VerifyMessage(ISubscriber destination)
{
var status = new HeartBeatStatus();
var task = new Task<CheckResult>(() =>
{
Console.WriteLine("VerifyMessage(Start): {0} - {1}", DateTime.Now, WarningTimeout);
Thread.Sleep(WarningTimeout - 500);
Console.WriteLine("VerifyMessage(Success): {0}", DateTime.Now);
if (CheckMessages(destination))
{
return CheckResult.Success;
}
Console.WriteLine("VerifyMessage(Pre-Warning): {0} - {1}", DateTime.Now, ErrorTimeout);
Thread.Sleep(ErrorTimeout - 500);
Console.WriteLine("VerifyMessage(Warning): {0}", DateTime.Now);
if (CheckMessages(destination))
{
return CheckResult.Warning;
}
return CheckResult.Error;
});
task.Start();
task.Wait();
status.Status = task.Result;
return status;
}
with the following unit test:
public void HeartBeat_Should_ReturnWarning_When_MockReturnsWarning()
{
// Arrange
var heartbeat = new SocketToSocketHeartbeat(_sourceSubscriber.Object, _destinationSubscriber.Object);
heartbeat.SetTaskConfiguration(this.ConfigurationHB1ToHB2_ValidConfiguration());
// Simulate the message being delayed to destination subscriber.
_destinationSubscriber.Setup(foo => foo.ReceivedMessages).Returns(DelayDelivery(3000, Message_HB1ToHB2()));
// Act
var healthStatus = heartbeat.Execute();
// Assert
Assert.AreEqual(CheckResult.Warning, healthStatus.Status);
}
Message_HB1ToHB2() just returns a string of characters and the "Delay Delivery" method is
private List<NcsMessage> DelayDelivery(int delay, string message)
{
var sent = DateTime.Now;
var msg = new NcsMessage()
{
SourceSubscriber = "HB1",
DestinationSubscriber = "HB2",
SentOrReceived = sent,
Message = message
};
var messages = new List<NcsMessage>();
messages.Add(msg);
Console.WriteLine("DelayDelivery: {0}", DateTime.Now);
Thread.Sleep(delay);
Console.WriteLine("DelayDelivery: {0}", DateTime.Now);
return messages;
}
I'm using Moq as the mocking framework and MSTest as the testing framework. Whenever I run the unit test, I get the following output:
DelayDelivery: 04/04/2013 15:50:33
DelayDelivery: 04/04/2013 15:50:36
VerifyMessage(Start): 04/04/2013 15:50:36 - 3000
VerifyMessage(Success): 04/04/2013 15:50:38
Beyond the obvious "code smell" using the Thread.Sleep in the methods above, the result of the unit test is not what I'm trying to accomplish.
Can anyone suggest a better/accurate way to use the Moq framework to simulate a delay in "delivery" of the message. I've left out some of the "glue" code and only included the relevant parts. Let me know if something I've left out that prevents you from being able to understand the question.
If you want a Moq mock to just sit and do nothing for a while you can use a callback:
Mock<IFoo> mockFoo = new Mock<IFoo>();
mockFoo.Setup(f => f.Bar())
.Callback(() => Thread.Sleep(1000))
.Returns("test");
string result = mockFoo.Object.Bar(); // will take 1 second to return
Assert.AreEqual("test", result);
I've tried that in LinqPad and if you adjust the Thread.Sleep() the execution time varies accordingly.
When you setup your mock you can tell the thread to sleep in the return func:
Mock<IMyService> myService = new Mock<IMyService>();
myService.Setup(x => x.GetResultDelayed()).Returns(() => {
Thread.Sleep(100);
return "result";
});
If running asynchronous code, Moq has the ability to delay the response with the second parameter via a TimeSpan
mockFooService
.Setup(m => m.GetFooAsync())
.ReturnsAsync(new Foo(), TimeSpan.FromMilliseconds(500)); // Delay return for 500 milliseconds.
If you need to specify a different delay each time the method is called, you can use .SetupSequence like
mockFooService
.SetupSequence(m => m.GetFooAsync())
.Returns(new Foo())
.Returns(Task.Run(async () =>
{
await Task.Delay(500) // Delay return for 500 milliseconds.
return new Foo();
})
I could not get Moq version to work, so I ended up making something like this:
a small example using WaitHandle:
[TestFixture]
public class EventWaitHandleTests
{
class Worker {
private volatile bool _shouldStop;
public EventWaitHandle WaitHandleExternal;
public void DoWork ()
{
while (!_shouldStop)
{
Console.WriteLine("worker thread: working...");
Thread.Sleep(1000);
WaitHandleExternal.Set();
}
}
public void RequestStop()
{
_shouldStop = true;
}
}
[Test]
public void WaitForHandleEventTest()
{
EventWaitHandle _waitHandle = new AutoResetEvent (false); // is signaled value change to true
// start a thread which will after a small time set an event
Worker workerObject = new Worker ();
workerObject.WaitHandleExternal = _waitHandle;
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start();
Console.WriteLine ("Waiting...");
_waitHandle.WaitOne(); // Wait for notification
Console.WriteLine ("Notified");
// Stop the worker thread.
workerObject.RequestStop();
}
}
I like and voted for serup's solution. My answer is a version of his converted for use as a library.
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// support halting a workflow and waiting for a finish request
/// </summary>
public class MockWorker
{
private readonly DateTime? _end;
private volatile bool _shouldStop;
/// <summary>
/// Create a worker object
/// </summary>
/// <param name="timeoutInMilliseconds">How long before DoWork will timeout. default - Null will not timeout.</param>
public MockWorker(int? timeoutInMilliseconds = null)
{
if (timeoutInMilliseconds.HasValue)
_end = DateTime.Now.AddMilliseconds(timeoutInMilliseconds.Value);
}
/// <summary>
/// Instruct DoWork to complete
/// </summary>
public void RequestStop()
{
_shouldStop = true;
}
/// <summary>
/// Do work async will run until either timeoutInMilliseconds is exceeded or RequestStop is called.
/// </summary>
public async Task DoWorkAsync()
{
while (!_shouldStop)
{
await Task.Delay(100);
if (_end.HasValue && _end.Value < DateTime.Now)
throw new AssertFailedException("Timeout");
}
}
/// <summary>
/// Do work async will run until either timeoutInMilliseconds is exceeded or RequestStop is called.
/// </summary>
/// <typeparam name="T">Type of value to return</typeparam>
/// <param name="valueToReturn">The value to be returned</param>
/// <returns>valueToReturn</returns>
public async Task<T> DoWorkAsync<T>(T valueToReturn)
{
await DoWorkAsync();
return valueToReturn;
}
}
I had a similiar situation, but with an Async method. What worked for me was to do the following:
mock_object.Setup(scheduler => scheduler.MakeJobAsync())
.Returns(Task.Run(()=> { Thread.Sleep(50000); return Guid.NewGuid().ToString(); }));

Get async callback value in unittest debug

I got UnitTest method which calls method with callback
[Test]
public void GetUserAsyncTest()
{
User result;
_restTest.GetUserAsync((user) =>
{
result = user;
});
Assert.AreEqual("xy", result.Email);
}
This is my method signature
/// <summary>
/// Retrieve the User details for the currently authenticated User
/// </summary>
/// <param name="callback">Method to call upon successful completion</param>
public void GetUserAsync(Action<User> callback)
How can I test this and get value from callback? Currently my result is always null which is logical.
Use an event to wait until the async method has finished:
[Test]
public void GetUserAsyncTest()
{
//Action<User> user = null;
User result;
ManualResetEvent waitEvent = new ManualResetEvent(false);
_restTest.GetUserAsync((user) =>
{
result = user;
waitEvent.Set();
});
waitEvent.WaitOne();
Assert.AreEqual("xy", result.Email);
}
Also changed user.Email to result.Email. Suspected you want to check the result variable.

Categories