Example #1
0
        public void SocketServerTest()
        {
            var         results = new List <string>();
            int         port    = 12345;
            TcpListener server  = new TcpListener(System.Net.IPAddress.Any, port);

            var a = Scheduler.Start(ServerTask(server, 1));

            var b = Future.RunInThread(new Action(() => {
                System.Diagnostics.Debug.WriteLine("Connecting...");
                using (TcpClient client = new TcpClient("localhost", port)) {
                    using (StreamReader reader = new StreamReader(client.GetStream(), Encoding.ASCII)) {
                        System.Diagnostics.Debug.WriteLine("Connected. Receiving data...");
                        string result = reader.ReadToEnd();
                        System.Diagnostics.Debug.WriteLine("Data received.");
                        results.Add(result);
                    }
                }
                System.Diagnostics.Debug.WriteLine("Disconnected.");
            }));

            while (!b.Completed)
            {
                Scheduler.Step();
            }

            Assert.AreEqual(new string[] { "Hello, world!" }, results.ToArray());
        }
Example #2
0
        public void RunInThreadTest()
        {
            int numIterations = 1000000 * 20;
            var buf           = new ValueHolder();
            var f             = Future.RunInThread(new Func <ValueHolder, int, int>(WorkerThread), buf, numIterations);

            while (!f.Completed)
            {
                Scheduler.Step();
            }
            Assert.AreEqual(42, f.Result);
            Assert.AreEqual(numIterations, buf.Value);
        }
Example #3
0
        public void WrapsExceptionsFromInsideWorkerThreads()
        {
            var f = Future.RunInThread(new Action(CrashyWorkerThread));

            while (!f.Completed)
            {
                Scheduler.Step();
            }
            try {
                var _ = f.Result;
                Assert.Fail("Exception was not raised");
            } catch (Exception ex) {
                Assert.AreEqual("maple syrup", ex.InnerException.Message);
            }
        }
Example #4
0
        public void MultipleThreadTest()
        {
            int numIterations = 1000000;
            int numWorkers    = 50;

            var buf = new ValueHolder();
            var fn  = new Func <ValueHolder, int, int>(WorkerThread);

            var futures = new List <IFuture>();

            for (int i = 0; i < numWorkers; i++)
            {
                futures.Add(Future.RunInThread(fn, buf, numIterations));
            }

            var f = Future.WaitForAll(futures);

            while (!f.Completed)
            {
                Scheduler.Step();
            }

            Assert.AreEqual(numIterations * numWorkers, buf.Value);
        }
Example #5
0
        public static IEnumerator <object> IssueRequest(HttpWebRequest request)
        {
            var fResponse = new Future <HttpWebResponse>();

            ThreadPool.QueueUserWorkItem(
                (__) => {
                try {
                    request.BeginGetResponse(
                        (ar) => {
                        try {
                            var _ = (HttpWebResponse)request.EndGetResponse(ar);
                            fResponse.SetResult(_, null);
                        } catch (Exception ex) {
                            fResponse.SetResult(null, ex);
                        }
                    }, null
                        );
                } catch (Exception ex_) {
                    fResponse.SetResult(null, ex_);
                }
            }
                );

            yield return(fResponse);

            if (fResponse.Failed)
            {
                throw new RequestFailedException(fResponse.Error);
            }

            using (var response = fResponse.Result) {
                var fResponseStream = Future.RunInThread(
                    () => response.GetResponseStream()
                    );
                yield return(fResponseStream);

                Encoding encoding = AsyncTextReader.DefaultEncoding;
                if (!string.IsNullOrEmpty(response.CharacterSet))
                {
                    encoding = Encoding.GetEncoding(response.CharacterSet);
                }

                string responseText;
                using (var stream = fResponseStream.Result)
                    using (var adapter = new AsyncTextReader(new StreamDataAdapter(stream, false), encoding)) {
                        var fText = adapter.ReadToEnd();

                        yield return(fText);

                        responseText = fText.Result;
                    }

                var cookies = new Cookie[response.Cookies.Count];
                response.Cookies.CopyTo(cookies, 0);

                yield return(new Result(new Response {
                    Body = responseText,
                    ContentType = response.ContentType,
                    StatusCode = response.StatusCode,
                    StatusDescription = response.StatusDescription,
                    Cookies = cookies
                }));
            }
        }