This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a HttpTextWriter that can send logs over http.
There is no public API to create raw sockets with watchOS, so we can't use Tcp directly, thus the need for using Http.
- Loading branch information
1 parent
1b5be08
commit ffe2a6d
Showing
3 changed files
with
142 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// HttpTextWriter.cs: Class to report test results using http requests | ||
// | ||
// Authors: | ||
// Rolf Bjarne Kvinge <rolf@xamarin.com> | ||
// | ||
// Copyright 2016 Xamarin Inc. | ||
// | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
#if __UNIFIED__ | ||
using Foundation; | ||
#else | ||
using MonoTouch.Foundation; | ||
#endif | ||
|
||
namespace MonoTouch.NUnit { | ||
class HttpTextWriter : TextWriter | ||
{ | ||
public string HostName; | ||
public int Port; | ||
|
||
TaskCompletionSource<bool> finished = new TaskCompletionSource<bool> (); | ||
TaskCompletionSource<bool> closed = new TaskCompletionSource<bool> (); | ||
StringBuilder log = new StringBuilder (); | ||
|
||
public Task FinishedTask { | ||
get { | ||
return finished.Task; | ||
} | ||
} | ||
|
||
public override Encoding Encoding { | ||
get { | ||
return Encoding.UTF8; | ||
} | ||
} | ||
|
||
public override void Close () | ||
{ | ||
closed.SetResult (true); | ||
Task.Run (async () => | ||
{ | ||
await finished.Task; | ||
base.Close (); | ||
}); | ||
} | ||
|
||
Task SendData (string action, string uploadData) | ||
{ | ||
var url = NSUrl.FromString ("http://" + HostName + ":" + Port + "/" + action); | ||
var request = new NSMutableUrlRequest (url); | ||
request.HttpMethod = "POST"; | ||
return NSUrlSession.SharedSession.CreateUploadTaskAsync (request, NSData.FromString (uploadData)); | ||
} | ||
|
||
async void SendThread () | ||
{ | ||
try { | ||
await SendData ("Start", ""); | ||
await closed.Task; | ||
await SendData ("Finish", log.ToString ()); | ||
} catch (Exception ex) { | ||
Console.WriteLine ("HttpTextWriter failed: {0}", ex); | ||
} finally { | ||
finished.SetResult (true); | ||
} | ||
} | ||
|
||
public void Open () | ||
{ | ||
new Thread (SendThread) | ||
{ | ||
IsBackground = true, | ||
}.Start (); | ||
} | ||
|
||
public override void Write (char value) | ||
{ | ||
Console.Out.Write (value); | ||
log.Append (value); | ||
} | ||
|
||
public override void Write (char [] buffer) | ||
{ | ||
Console.Out.Write (buffer); | ||
log.Append (buffer); | ||
} | ||
|
||
public override void WriteLine (string value) | ||
{ | ||
Console.Out.WriteLine (value); | ||
log.AppendLine (value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters