aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.UnitTesting/RemoteRunner_TST.cs
blob: 0fdd437f0dec15e658f23d27344227acadf1c562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.Transport;
using Tango.Transport.Transporters;
using Tango.Transport.Adapters;
using Tango.PMR.IO;
using Tango.PMR;
using Google.Protobuf;
using Tango.Transport.Discovery;
using Tango.PMR.Discovery;

namespace Tango.UnitTesting
{
    [TestClass]
    [TestCategory("Remote Runner")]
    public class RemoteRunner_TST 
    {
        [TestMethod]
        public void Run_Remote_Runner_Connect_Upload_And_Execute_Process()
        {
            Process runner = Process.Start(Path.Combine(Helper.GetBuildPath(), "Tango.RemoteRunner.UI.exe"));
            runner.WaitForInputIdle();

            Thread.Sleep(2000);

            UdpDiscoveryClient<BasicDiscoveryMessage> discoveryClient = new UdpDiscoveryClient<BasicDiscoveryMessage>(2018);
            var discoveryResponse = discoveryClient.Discover().Result;

            ITransportAdapter adapter = new TcpTransportAdapter(discoveryResponse.Address, discoveryResponse.Message.Port);
            ITransporter transporter = new BasicTransporter(adapter);
            transporter.Connect().Wait();

            Thread.Sleep(1000);

            String uploadFileName = "RemoteRunnerExeTest.zip";
            String executeFileName = "RemoteRunnerExeTest.exe";

            System.IO.FileInfo exeFile = new System.IO.FileInfo(Helper.GetResourcePath(uploadFileName));

            var uploadResponse = transporter.SendRequest<FileUploadRequest, FileUploadResponse>(new FileUploadRequest()
            {
                Path = uploadFileName,
                Length = exeFile.Length,
            }).Result.Message;

            int currentFilePosition = 0;

            do
            {
                using (FileStream fs = new FileStream(exeFile.FullName, FileMode.Open))
                {
                    fs.Position = currentFilePosition;

                    FileChunkUploadRequest chunkRequest = new FileChunkUploadRequest();
                    chunkRequest.UploadID = uploadResponse.UploadID;

                    long remaining = exeFile.Length - currentFilePosition;

                    byte[] buffer = new byte[uploadResponse.MaxChunkLength < remaining ? uploadResponse.MaxChunkLength : remaining];
                    fs.Read(buffer, 0, buffer.Length);
                    chunkRequest.Buffer = ByteString.CopyFrom(buffer);

                    transporter.SendRequest<FileChunkUploadRequest, FileChunkUploadResponse>(chunkRequest).Wait();

                    currentFilePosition += buffer.Length;
                }

            } while (currentFilePosition < exeFile.Length);

            Thread.Sleep(1000);

            var response = transporter.SendRequest<ExecuteProcessRequest, ExecuteProcessResponse>(new ExecuteProcessRequest()
            {
                UploadID = uploadResponse.UploadID,
                FileName = executeFileName,
            }, TimeSpan.FromSeconds(30)).Result.Message;

            Thread.Sleep(5000);

            transporter.SendRequest<KillProcessRequest, KillProcessResponse>(new KillProcessRequest()
            {
                ProcessID = response.ProcessID,
            }).Wait();

            transporter.Disconnect().Wait();

            runner.Kill();
        }
    }
}