コード例 #1
0
        public void ResourceMonitor_WhenResourceMonitoredByMultipleParties_CorrectCallbacksAreCalled()
        {
            using (UnitTestFileResourceMonitor monitor = new UnitTestFileResourceMonitor())
            {
                UnitTestTextFile file1     = new UnitTestTextFile(true, true, "contents1");
                FileResource     resource1 = new FileResource(file1, "folder", "name1");
                UnitTestTextFile file2     = new UnitTestTextFile(true, true, "contents2");
                FileResource     resource2 = new FileResource(file2, "folder", "name2");

                bool handlerCalled1              = false;
                bool handlerCalled12             = false;
                bool handlerCalled2              = false;
                ResourceUpdatedHandler handler1  = eventArgs => { handlerCalled1 = true; };
                ResourceUpdatedHandler handler12 = eventArgs => { handlerCalled12 = true; };
                ResourceUpdatedHandler handler2  = eventArgs => { handlerCalled2 = true; };

                Assert.True(monitor.TryStartMonitoring(new[] { resource1 }, handler1));
                Assert.True(monitor.TryStartMonitoring(new[] { resource1, resource2 }, handler12));
                Assert.True(monitor.TryStartMonitoring(new[] { resource2 }, handler2));
                DateTime monitoringStartMoment = DateTime.UtcNow;
                while (!handlerCalled1 || !handlerCalled2 || !handlerCalled12)
                {
                    if (DateTime.UtcNow >= monitoringStartMoment + m_monitoringMaxTime)
                    {
                        break;
                    }
                }

                Assert.True(handlerCalled1 && handlerCalled2 && handlerCalled12);
                handlerCalled1  = false;
                handlerCalled12 = false;
                handlerCalled2  = false;

                monitoringStartMoment = DateTime.UtcNow;
                file1.WriteAllBytes("", new byte[] { 1 });
                while (!handlerCalled1 || !handlerCalled12)
                {
                    if (DateTime.UtcNow >= monitoringStartMoment + m_monitoringMaxTime)
                    {
                        break;
                    }
                }

                Assert.True(handlerCalled1);
                Assert.True(handlerCalled12);
                Assert.False(handlerCalled2);
            }
        }
コード例 #2
0
        public void ResourceMonitor_WhenResourceUpdates_CausesCallbackToBeCalled()
        {
            using (UnitTestFileResourceMonitor monitor = new UnitTestFileResourceMonitor())
            {
                UnitTestTextFile file     = new UnitTestTextFile(true, true, "contents");
                FileResource     resource = new FileResource(file, "folder", "name");
                Assert.True(monitor.TryStartMonitoring(new[] { resource }, PerformEvent));
                m_handlerCalledIndicator = false;
                file.WriteAllBytes("", new byte[] { 0, 1 });
                DateTime monitoringStartMoment = DateTime.UtcNow;
                TimeSpan monitoringMaxTime     = new TimeSpan(0, 0, 10);
                while (!m_handlerCalledIndicator)
                {
                    if (DateTime.UtcNow >= monitoringStartMoment + monitoringMaxTime)
                    {
                        break;
                    }
                }

                Assert.True(m_handlerCalledIndicator);
            }
        }