Example #1
0
        private static void Main(string[] args)
        {
            // create the event loop
            var loop = new EventLoop();

            // start it with a callback
            loop.Start(() => Console.WriteLine("Event loop has started"));

            // start a timer
            loop.SetTimeout(() => Console.WriteLine("Hello, I am a timeout happening after 1 second"), TimeSpan.FromSeconds(1));
            loop.SetInterval(() => Console.WriteLine("Hello, I am an interval happening every 2 seconds"), TimeSpan.FromSeconds(2));

            // read the app.config
            var appConfigFile = new FileInfo("NLoop.TestApp.exe.config");
            var promise = appConfigFile.ReadAllBytes(loop);
            promise.Then(content => Console.WriteLine("Success!! read {0} bytes from app.config", content.Length), reason => Console.WriteLine("Dread!! got an error: {0}", reason));

            // send a web request
            var httpClient = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com/");
            var httpPromise = httpClient.Send(loop, request);
            httpPromise.Then(content => Console.WriteLine("Success!! read {0} from google.com", content.StatusCode), reason => Console.WriteLine("Dread!! got an error: {0}", reason));
            //httpPromise.Cancel();

            // wait
            Console.WriteLine("Event loop is processing, press any key to exit");
            Console.Read();

            // we are done, dispose the loop so resources will be cleaned up
            loop.Dispose();
        }
        public void ReadAllBytes()
        {
            // arrange
            var scheduler = new Mock<IScheduler>();
            scheduler.Setup(s => s.Schedule(It.IsAny<Action>())).Callback<Action>(action => Task.Run(action));
            var fileInfo = new FileInfo("NLoop.IO.Tests.dll");

            // assert parameter checking
            Assert.That(() => ((FileInfo) null).ReadAllBytes(scheduler.Object), Throws.InstanceOf<ArgumentNullException>());
            Assert.That(() => fileInfo.ReadAllBytes(null), Throws.InstanceOf<ArgumentNullException>());

            // check file not found
            var notExistingFile = new FileInfo("Idonotexist.file");
            var notExistingPromise = notExistingFile.ReadAllBytes(scheduler.Object);
            PromiseAssert.Rejects(notExistingPromise);

            // check file read
            var existingPromise = fileInfo.ReadAllBytes(scheduler.Object);
            PromiseAssert.Resolves(existingPromise);
        }
        public void ReadAllBytes()
        {
            // Type
            var @this = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_IO_FileInfo_ReadAllBytes.txt"));

            // Intialization
            using (FileStream stream = @this.Create())
            {
                stream.WriteByte(0);
            }

            // Examples
            byte[] value = @this.ReadAllBytes(); // return byte[] { 0 };

            // Unit Test
            Assert.AreEqual(0, value[0]);
        }