コード例 #1
0
        public void PhraseCache_PurgeOrphaned()
        {
            // Verify that the cache deletes orhpaned files.

            string path1;
            string path2;

            DeleteFolder();

            try
            {
                var settings = new SpeechEngineSettings();

                settings.PhraseCacheFolder   = PhraseCachePath;
                settings.OneTimePhraseFolder = OneTimePath;
                settings.PhraseFolderFanout  = 10;
                settings.PhrasePurgeInterval = TimeSpan.FromSeconds(0.5);
                settings.MaxOneTimePhraseTTL = TimeSpan.FromSeconds(1);

                SpeechEngine.Start(settings);

                // Add couple files to the cache, one at the root and the second
                // in a subfolder.  Then force a cache purge and then verify that
                // the files were deleted and the index file still exists.

                path1 = Path.Combine(PhraseCachePath, "test1.dat");
                File.WriteAllText(path1, "test1");

                path2 = Path.Combine(PhraseCachePath, "0", "test2.dat");
                File.WriteAllText(path2, "test2");

                // Call the speech engine's background task handler so it
                // can handle cache purging and then verify that the
                // phrase was actually purged.

                Thread.Sleep(TimeSpan.FromSeconds(2));
                SpeechEngine.OnBkTask();
                Helper.WaitFor(() => !File.Exists(path1), TimeSpan.FromSeconds(5));

                Assert.IsFalse(File.Exists(path1));
                Assert.IsFalse(File.Exists(path2));

                // Make sure we didn't purge the index file by accident.

                Assert.IsTrue(File.Exists(Path.Combine(PhraseCachePath, PhraseCache.IndexFileName)));
            }
            finally
            {
                SpeechEngine.Stop();
                DeleteFolder();
            }
        }
コード例 #2
0
        public void PhraseCache_PurgeCache_WithSubfolders()
        {
            // Verify that the cache purges expired phrases within subfolders.

            Phrase phrase;

            DeleteFolder();

            try
            {
                var settings = new SpeechEngineSettings();

                settings.PhraseCacheFolder   = PhraseCachePath;
                settings.OneTimePhraseFolder = OneTimePath;
                settings.PhraseFolderFanout  = 10;
                settings.PhrasePurgeInterval = TimeSpan.FromSeconds(0.5);
                settings.MaxPhraseTTL        = TimeSpan.FromSeconds(1);

                SpeechEngine.Start(settings);

                // Create a phrase and add it to the cache.

                phrase      = Phrase.PhoneText("Hello World!");
                phrase.Path = SpeechEngine.PhraseCache.GetNextPhrasePath(phrase);
                File.WriteAllBytes(phrase.Path, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
                SpeechEngine.PhraseCache.AddPhrase(phrase);

                // Wait five seconds for the phrase to expire.

                Thread.Sleep(TimeSpan.FromSeconds(5));

                // Call the speech engine's background task handler so it
                // can handle cache purging and then verify that the
                // phrase was actually purged.

                SpeechEngine.OnBkTask();
                Helper.WaitFor(() => !File.Exists(phrase.Path), TimeSpan.FromSeconds(5));

                Assert.IsNull(SpeechEngine.PhraseCache.FindPhrase(phrase));
                Assert.IsFalse(File.Exists(phrase.Path));
            }
            finally
            {
                SpeechEngine.Stop();
                DeleteFolder();
            }
        }
コード例 #3
0
        public void PhraseCache_Blast()
        {
            // Blast the cache with traffic on multiple threads to make sure
            // that nothing bad happens.

            // Verify that the cache purges expired phrases.

            bool stop  = false;
            int  count = 0;

            DeleteFolder();

            try
            {
                var settings = new SpeechEngineSettings();

                settings.PhraseCacheFolder   = PhraseCachePath;
                settings.OneTimePhraseFolder = OneTimePath;
                settings.PhraseFolderFanout  = 100;
                settings.PhrasePurgeInterval = TimeSpan.FromSeconds(0.5);
                settings.MaxPhraseTTL        = TimeSpan.FromSeconds(1);
                settings.MaxOneTimePhraseTTL = TimeSpan.FromSeconds(1);

                SpeechEngine.Start(settings);

                // Simulate a background task thread.

                Helper.EnqueueAction(
                    () =>
                {
                    while (!stop)
                    {
                        SpeechEngine.OnBkTask();
                        Thread.Sleep(100);
                    }
                });

                // Create 5 threads that cache phrases.

                for (int i = 0; i < 5; i++)
                {
                    Helper.EnqueueAction(
                        () =>
                    {
                        while (!stop)
                        {
                            var phrase = Phrase.PhoneText("Hello World: {0}", Interlocked.Increment(ref count));

                            phrase.Path = SpeechEngine.PhraseCache.GetNextPhrasePath(phrase);
                            File.WriteAllBytes(phrase.Path, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
                            SpeechEngine.PhraseCache.AddPhrase(phrase);
                            Thread.Sleep(1000);
                        }
                    });
                }

                // Create 5 threads that create one-time phrases.

                for (int i = 0; i < 5; i++)
                {
                    Helper.EnqueueAction(
                        () =>
                    {
                        while (!stop)
                        {
                            var phrase = Phrase.PhoneText("Hello World: {0}", Interlocked.Increment(ref count));

                            phrase.Path = SpeechEngine.PhraseCache.GetNextOneTimePath(phrase);
                            File.WriteAllBytes(phrase.Path, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
                            Thread.Sleep(1000);
                        }
                    });
                }

                // Let the thing run for a while before signalling the threads to stop.

                Thread.Sleep(TimeSpan.FromSeconds(60));
                stop = true;

                // Give the threads a chance to stop.

                Thread.Sleep(TimeSpan.FromSeconds(5));
            }
            finally
            {
                SpeechEngine.Stop();
                DeleteFolder();
            }
        }