예제 #1
0
        private static GenericArray <ScriptLibrary> OpenFirestormScriptLibrarys()
        {
            var firestormScriptLibraries = new GenericArray <ScriptLibrary>();

            while (
                MessageBox.Show(
                    "Select additional firestorm scriptlibrary_* data to draw documentation from or click no to continue.",
                    "Select Firestorm Library Data", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                var dialog = new OpenFileDialog
                {
                    Filter          = "Firestorm Highlight File (*.xml) | *.xml",
                    CheckFileExists = true,
                    Multiselect     = true
                };

                dialog.ShowDialog();

                var xmlReaderSettings = new XmlReaderSettings {
                    CloseInput = true
                };
                foreach (var filename in dialog.FileNames)
                {
                    using (var reader = XmlReader.Create(File.OpenRead(filename), xmlReaderSettings))
                    {
                        firestormScriptLibraries.Add(ScriptLibrary.Read(reader));
                    }
                }
            }

            return(firestormScriptLibraries);
        }
예제 #2
0
        public async Task SimpleScenario()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var service    = new LockService(library, new Internals.Locks.IdentifierGenerator());

            var root = Guid.NewGuid().ToString().Substring(0, 8);
            var key  = new Key(root, "name", TimeSpan.FromSeconds(1));

            var result = false;
            var db     = connection.GetDatabase();

            await ResetKeys(db, key);

            try
            {
                await service.LockAsync(key, c =>
                {
                    result = true;
                    return(Task.CompletedTask);
                }, CancellationToken.None);

                Assert.True(result);
                Assert.False(await db.KeyExistsAsync(key.GetLockKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueSetKey()));
            }
            finally
            {
                await ResetKeys(db, key);
            }
        }
예제 #3
0
        public async Task CancelScenario()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var service    = new LockService(library, new Internals.Locks.IdentifierGenerator());

            var root = Guid.NewGuid().ToString().Substring(0, 8);
            var key  = new Key(root, "name", TimeSpan.FromSeconds(2));

            var db = connection.GetDatabase();

            await ResetKeys(db, key);

            try
            {
                using var cancel = new CancellationTokenSource();
                cancel.CancelAfter(500);
                try
                {
                    await service.LockAsync(key, c => Task.Delay(1000, c), cancel.Token);
                }
                catch (Exception e)
                {
                    Assert.IsType <TaskCanceledException>(e.InnerException?.InnerException ?? e.InnerException ?? e);
                }

                Assert.False(await db.KeyExistsAsync(key.GetLockKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueSetKey()));
            }
            finally
            {
                await ResetKeys(db, key);
            }
        }
        /// <summary>
        /// Opens help content from the ScriptLibrary.
        /// </summary>
        /// <param name="subClass">The subClass used in @help().</param>
        private static void DoHelp(string subClass)
        {
            if (subClass.EndsWith(")") == true)
            {
                subClass = subClass.Remove(subClass.Length - 1, 1);
            }
            Chat.AddLine(new Chat.ChatMessage("[HELP]", ScriptLibrary.GetHelpContent(subClass, 20), "0", Chat.ChatMessage.MessageTypes.CommandMessage));

            IsReady = true;
        }
        public ScriptLibrary AddFromDirectory(string path)
        {
            var directory = new DirectoryInfo(path);

            if ( directory.Exists && !scriptLibraries.ContainsKey(directory.Name))
            {
                var newScriptLibrary = new ScriptLibrary(new DirectoryScriptSourceProvider(directory,extension), compilationContextFactory);
                scriptLibraries.Add(directory.Name,newScriptLibrary);
            }

            return scriptLibraries.ValueOrDefault(directory.Name);
        }
예제 #6
0
        public async Task ConcurrencyScenario()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var service    = new LockService(library, new Internals.Locks.IdentifierGenerator());

            var root = Guid.NewGuid().ToString().Substring(0, 8);
            var key  = new Key(root, "name", TimeSpan.FromSeconds(5));

            var db = connection.GetDatabase();

            var events = new ConcurrentQueue <string>();
            var concurrencyScenarioCounter = 0;
            var errorCount = 0;
            var counter    = 0;

            await ResetKeys(db, key);

            try
            {
                var watch = new Stopwatch();
                var tasks = Enumerable.Range(0, 100).Select(i => service.LockAsync(key, async c =>
                {
                    Interlocked.Increment(ref concurrencyScenarioCounter);
                    events.Enqueue($"{i:d3} in  at {watch.ElapsedMilliseconds:d4}ms thread id {Thread.CurrentThread.ManagedThreadId}");
                    if (concurrencyScenarioCounter != 1)
                    {
                        watch.Stop();
                        Interlocked.Increment(ref errorCount);
                        throw new Exception($"Lock failed after {watch.ElapsedMilliseconds}" + Environment.NewLine + string.Join(Environment.NewLine, events));
                    }
                    Interlocked.Increment(ref counter);
                    await Task.Delay(10);
                    events.Enqueue($"{i:d3} out at {watch.ElapsedMilliseconds:d4}ms thread id {Thread.CurrentThread.ManagedThreadId}");
                    Interlocked.Decrement(ref concurrencyScenarioCounter);
                }, CancellationToken.None));

                watch.Start();
                await Task.WhenAll(tasks);

                Assert.Equal(100, counter);
                Assert.Equal(0, errorCount);

                Assert.False(await db.KeyExistsAsync(key.GetLockKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueSetKey()));
            }
            finally
            {
                await ResetKeys(db, key);
            }
        }
예제 #7
0
        public async Task SubscribeTests()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var db         = connection.GetDatabase();

            var root = Guid.NewGuid().ToString().Substring(0, 8);
            var key  = new Key(root, "name", TimeSpan.FromSeconds(2));

            using var context = new LockState(key, "identifier", CancellationToken.None);
            await library.SubscribeAsync(context);

            await db.PublishAsync(key.GetLockChannelKey(), "identifier");

            await Task.Delay(500);

            Assert.Equal(State.WithKey, context.State);//proof that the message was received
            await library.UnSubscribeAsync(context);
        }
예제 #8
0
        /// <summary>
        /// A helper for registering script tags on an MVC View page.
        /// </summary>
        public static string RegisterJS(this System.Web.Mvc.HtmlHelper helper, ScriptLibrary scriptLib) {
            //get the directory where the scripts are
            string scriptRoot = VirtualPathUtility.ToAbsolute("~/Content/Scripts");
            string scriptFormat="<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";
            
            string scriptLibFile="";
            string result = "";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            
            //all of the script tags
            if (scriptLib == ScriptLibrary.Ajax) {
                sb.AppendLine(RegisterScript(helper, "MicrosoftAjax.js"));
                sb.AppendLine(RegisterScript(helper, "MicrosoftAjaxMVC.js"));

            } else {
                sb.AppendLine(RegisterScript(helper, Enum.GetName(typeof(ScriptLibrary),scriptLib)+".js"));

            }
            result = sb.ToString();
            return result;
        }
예제 #9
0
        public async Task GetKeySituationTests(string priorValue, string waiting, int expected)
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var db         = connection.GetDatabase();

            //SETUP
            var root       = Guid.NewGuid().ToString().Substring(0, 8);
            var parameters = new LockLuaParameters("id", new Key(root, "name", TimeSpan.FromMilliseconds(500)));

            try
            {
                await ResetKeys(db, parameters);

                if (!string.IsNullOrEmpty(priorValue))
                {
                    await db.StringSetAsync(parameters.LockKey, priorValue);
                }

                if (!string.IsNullOrEmpty(waiting))
                {
                    await db.HashSetAsync(parameters.LockWaitingSetKey, waiting, 0);
                }

                //ACT
                var result = await library.GetKeySituation(parameters);

                var exists = await db.HashExistsAsync(parameters.LockWaitingSetKey, parameters.Identifier);

                Assert.Equal(expected, result);
            }
            finally
            {
                await ResetKeys(db, parameters);
            }
        }
예제 #10
0
    /// <summary>
    /// A helper for registering script tags on an MVC View page.
    /// </summary>
    public static string RegisterJS(this System.Web.Mvc.HtmlHelper helper, ScriptLibrary scriptLib)
    {
        //get the directory where the scripts are
        string scriptRoot   = VirtualPathUtility.ToAbsolute("~/Content/Scripts");
        string scriptFormat = "<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";

        string scriptLibFile = "";
        string result        = "";

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        //all of the script tags
        if (scriptLib == ScriptLibrary.Ajax)
        {
            sb.AppendLine(RegisterScript(helper, "MicrosoftAjax.js"));
            sb.AppendLine(RegisterScript(helper, "MicrosoftAjaxMVC.js"));
        }
        else
        {
            sb.AppendLine(RegisterScript(helper, Enum.GetName(typeof(ScriptLibrary), scriptLib) + ".js"));
        }
        result = sb.ToString();
        return(result);
    }
 /// <summary>Retrives the of the file corresponding to the given library.</summary>
 /// <param name="library">Flag for the library to retreive.</param>
 public string ToName(ScriptLibrary library)
 {
     switch (library)
     {
         case ScriptLibrary.Core: return Core;
         case ScriptLibrary.Controls: return CoreControls;
         case ScriptLibrary.Lists: return CoreLists;
         case ScriptLibrary.JQuery: return JQuery;
         case ScriptLibrary.JQueryUi: return JQueryUi;
         case ScriptLibrary.JQueryCookie: return JQueryCookie;
         default: throw new Exception(string.Format("{0} not supported.", library.ToString()));
     }
 }
        /// <summary>Loads the specified library.</summary>
        /// <param name="library">Flag indicating the library to load.</param>
        /// <param name="callback">Callback to invoke upon completion.</param>
        public void LoadLibrary(ScriptLibrary library, Action callback)
        {
            // Setup initial conditions.
            if (IsLibraryLoaded(library))
            {
                Helper.Invoke(callback);
                return;
            }

            // Download script.
            ScriptLoader loader = new ScriptLoader();
            string url = Scripts.Url(library);
            loader.LoadComplete += delegate
                                       {
                                           CacheUrl(url);
                                           Helper.Invoke(callback);
                                       };
            loader.AddUrl(url);
            loader.Start();
        }
 /// <summary>Determines whether the specified library has been loaded.</summary>
 /// <param name="library">The library to look for.</param>
 public bool IsLibraryLoaded(ScriptLibrary library)
 {
     if (loadedUrls.Contains(Scripts.Url(library))) return true;
     if (IsDeclared(Scripts.Url(library))) return true;
     return false;
 }
예제 #14
0
 public FirestormLibraryData(ScriptLibrary library)
 {
     _scriptLibrary = library;
 }
예제 #15
0
        public async Task GetLockOrSubscribeTests(string priorValue, string waiting, bool expectedResult)
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var db         = connection.GetDatabase();

            //SETUP
            var root       = Guid.NewGuid().ToString().Substring(0, 8);
            var parameters = new LockLuaParameters("id", new Key(root, "name", TimeSpan.FromMilliseconds(500)));

            try
            {
                await ResetKeys(db, parameters);

                if (!string.IsNullOrEmpty(priorValue))
                {
                    await db.StringSetAsync(parameters.LockKey, priorValue, TimeSpan.FromMilliseconds(parameters.Timeout));
                }

                if (!string.IsNullOrEmpty(waiting))
                {
                    await db.HashSetAsync(parameters.LockWaitingSetKey, waiting, 0);

                    await db.KeyExpireAsync(parameters.LockWaitingSetKey, TimeSpan.FromMilliseconds(parameters.Timeout));

                    await db.ListRightPushAsync(parameters.LockWaitingListKey, waiting);

                    await db.KeyExpireAsync(parameters.LockWaitingListKey, TimeSpan.FromMilliseconds(parameters.Timeout));
                }

                //ACT
                var result = await library.GetLockOrAddToQueue(parameters);

                //ASSERT
                Assert.Equal(expectedResult, result);
                if (expectedResult)
                {
                    Assert.Equal(parameters.Identifier, await db.StringGetAsync(parameters.LockKey));
                    Assert.Equal(!string.IsNullOrEmpty(waiting), await db.KeyExistsAsync(parameters.LockWaitingListKey));
                    Assert.Equal(!string.IsNullOrEmpty(waiting), await db.KeyExistsAsync(parameters.LockWaitingSetKey));
                }
                else
                {
                    var lockKey = (string)await db.StringGetAsync(parameters.LockKey);

                    Assert.NotEmpty(lockKey);
                    Assert.NotEqual(parameters.Identifier, lockKey);
                    Assert.Contains(parameters.Identifier, await db.ListRangeAsync(parameters.LockWaitingListKey));
                    Assert.Contains(parameters.Identifier, (await db.HashGetAllAsync(parameters.LockWaitingSetKey)).Select(e => e.Name));
                }

                //expiration delay
                await Task.Delay((int)parameters.Timeout + 50);

                Assert.False(await db.KeyExistsAsync(parameters.LockKey));
                Assert.False(await db.KeyExistsAsync(parameters.LockWaitingListKey));
                Assert.False(await db.KeyExistsAsync(parameters.LockWaitingSetKey));
            }
            finally
            {
                await ResetKeys(db, parameters);
            }
        }
        /// <summary>Get the URL for the given library.</summary>
        /// <param name="library">Flag for the library to retreive.</param>
        public string Url(ScriptLibrary library)
        {
            // Setup initial conditions.
            bool useDebug = Helper.ScriptLoader.UseDebug;

            // Get the sub path.
            string path = string.Empty;
            switch (library)
            {
                case ScriptLibrary.JQuery:
                case ScriptLibrary.JQueryUi:
                case ScriptLibrary.JQueryCookie:
                    path = "JQuery/";
                    useDebug = false;
                    break;
            }

            // Retrieve the URL.
            return Helper.ScriptLoader.Url(path, ToName(library), useDebug);
        }
 public FirestormDocumentationScraper(XmlReader reader)
 {
     _scriptLibrary = ScriptLibrary.Read(reader);
 }
예제 #18
0
        public async Task FreeLockAndPopScriptTests(string priorValue, string waiting)
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var db         = connection.GetDatabase();

            //SETUP
            var root       = Guid.NewGuid().ToString().Substring(0, 8);
            var parameters = new LockLuaParameters("id", new Key(root, "name", TimeSpan.FromMilliseconds(500)));

            try
            {
                await ResetKeys(db, parameters);

                if (!string.IsNullOrEmpty(priorValue))
                {
                    await db.StringSetAsync(parameters.LockKey, priorValue, TimeSpan.FromMilliseconds(parameters.Timeout));
                }

                if (!string.IsNullOrEmpty(waiting))
                {
                    await db.HashSetAsync(parameters.LockWaitingSetKey, waiting, 0);

                    await db.ListRightPushAsync(parameters.LockWaitingListKey, waiting);
                }

                var message = "";
                await connection.GetSubscriber().SubscribeAsync(parameters.LockNotifKey, (a, b) => message = b);

                //ACT
                await library.FreeLockAndPop(parameters);

                var locked = await db.StringGetAsync(parameters.LockKey);

                Assert.NotEqual(parameters.Identifier, (string)locked);

                if (priorValue == parameters.Identifier)
                {
                    if (waiting != "")
                    {
                        Assert.Equal(waiting, locked);
                        Assert.NotNull(await db.KeyTimeToLiveAsync(parameters.LockKey));

                        //doesn't work somehow
                        //Assert.NotNull(await db.KeyTimeToLiveAsync(parameters.LockWaitingListKey));
                        //Assert.NotNull(await db.KeyTimeToLiveAsync(parameters.LockWaitingSetKey));

                        await Task.Delay(50);

                        Assert.Equal(message, waiting);

                        await Task.Delay(450);

                        Assert.False(await db.KeyExistsAsync(parameters.LockKey));
                        Assert.False(await db.KeyExistsAsync(parameters.LockWaitingListKey));
                        Assert.False(await db.KeyExistsAsync(parameters.LockWaitingSetKey));
                    }
                    else
                    {
                        Assert.Equal(RedisValue.Null, locked);
                        Assert.False(await db.KeyExistsAsync(parameters.LockKey));
                        Assert.False(await db.KeyExistsAsync(parameters.LockWaitingListKey));
                        Assert.False(await db.KeyExistsAsync(parameters.LockWaitingSetKey));
                    }
                }
            }
            finally
            {
                await ResetKeys(db, parameters);
            }
        }
 public FirestormDocumentationScraper(ScriptLibrary library)
 {
     _scriptLibrary = library;
 }
예제 #20
0
 public FirestormLibraryData(XmlReader reader)
 {
     _scriptLibrary = ScriptLibrary.Read(reader);
 }