예제 #1
0
        public void ScanResume()
        {
            using (var conn = Create(allowAdmin: true))
            {
                // only goes up to 3.*, so...
                Skip.IfMissingFeature(conn, nameof(RedisFeatures.Scan), x => x.Scan);
                var dbId   = TestConfig.GetDedicatedDB(conn);
                var db     = conn.GetDatabase(dbId);
                var prefix = Me();
                var server = GetServer(conn);
                server.FlushDatabase(dbId);
                int i;
                for (i = 0; i < 100; i++)
                {
                    db.StringSet(prefix + ":" + i, Guid.NewGuid().ToString());
                }

                var  expected = new HashSet <string>();
                long snapCursor = 0;
                int  snapOffset = 0, snapPageSize = 0;

                i = 0;
                var seq = server.Keys(dbId, prefix + ":*", pageSize: 15);
                foreach (var key in seq)
                {
                    if (i == 57)
                    {
                        snapCursor   = ((IScanningCursor)seq).Cursor;
                        snapOffset   = ((IScanningCursor)seq).PageOffset;
                        snapPageSize = ((IScanningCursor)seq).PageSize;
                        Log($"i: {i}, Cursor: {snapCursor}, Offset: {snapOffset}, PageSize: {snapPageSize}");
                    }
                    if (i >= 57)
                    {
                        expected.Add((string)key);
                    }
                    i++;
                }
                Log($"Expected: 43, Actual: {expected.Count}, Cursor: {snapCursor}, Offset: {snapOffset}, PageSize: {snapPageSize}");
                Assert.Equal(43, expected.Count);
                Assert.NotEqual(0, snapCursor);
                Assert.Equal(15, snapPageSize);

                // note: you might think that we can say "hmmm, 57 when using page-size 15 on an empty (flushed) db (so: no skipped keys); that'll be
                // offset 12 in the 4th page; you'd be wrong, though; page size doesn't *actually* mean page size; it is a rough analogue for
                // page size, with zero guarantees; in this particular test, the first page actually has 19 elements, for example. So: we cannot
                // make the following assertion:
                // Assert.Equal(12, snapOffset);

                seq = server.Keys(dbId, prefix + ":*", pageSize: 15, cursor: snapCursor, pageOffset: snapOffset);
                var seqCur = (IScanningCursor)seq;
                Assert.Equal(snapCursor, seqCur.Cursor);
                Assert.Equal(snapPageSize, seqCur.PageSize);
                Assert.Equal(snapOffset, seqCur.PageOffset);
                using (var iter = seq.GetEnumerator())
                {
                    var iterCur = (IScanningCursor)iter;
                    Assert.Equal(snapCursor, iterCur.Cursor);
                    Assert.Equal(snapOffset, iterCur.PageOffset);
                    Assert.Equal(snapCursor, seqCur.Cursor);
                    Assert.Equal(snapOffset, seqCur.PageOffset);

                    Assert.True(iter.MoveNext());
                    Assert.Equal(snapCursor, iterCur.Cursor);
                    Assert.Equal(snapOffset, iterCur.PageOffset);
                    Assert.Equal(snapCursor, seqCur.Cursor);
                    Assert.Equal(snapOffset, seqCur.PageOffset);

                    Assert.True(iter.MoveNext());
                    Assert.Equal(snapCursor, iterCur.Cursor);
                    Assert.Equal(snapOffset + 1, iterCur.PageOffset);
                    Assert.Equal(snapCursor, seqCur.Cursor);
                    Assert.Equal(snapOffset + 1, seqCur.PageOffset);
                }

                int count = 0;
                foreach (var key in seq)
                {
                    expected.Remove((string)key);
                    count++;
                }
                Assert.Empty(expected);
                Assert.Equal(43, count);
            }
        }
예제 #2
0
        public void Simple()
        {
            using (var conn = Create())
            {
                var server = conn.GetServer(TestConfig.Current.PrimaryServerAndPort);
                var script = LuaScript.Prepare("return redis.call('get', @key)");
                var loaded = script.Load(server);
                var key    = Me();

                var session = new ProfilingSession();

                conn.RegisterProfiler(() => session);

                var dbId = TestConfig.GetDedicatedDB();
                var db   = conn.GetDatabase(dbId);
                db.StringSet(key, "world");
                var result = db.ScriptEvaluate(script, new { key = (RedisKey)key });
                Assert.Equal("world", result.AsString());
                var loadedResult = db.ScriptEvaluate(loaded, new { key = (RedisKey)key });
                Assert.Equal("world", loadedResult.AsString());
                var val = db.StringGet(key);
                Assert.Equal("world", val);
                var s = (string)db.Execute("ECHO", "fii");
                Assert.Equal("fii", s);

                var cmds = session.FinishProfiling();
                var i    = 0;
                foreach (var cmd in cmds)
                {
                    Log("Command {0} (DB: {1}): {2}", i++, cmd.Db, cmd?.ToString()?.Replace("\n", ", "));
                }

                var all = string.Join(",", cmds.Select(x => x.Command));
                Assert.Equal("SET,EVAL,EVALSHA,GET,ECHO", all);
                Log("Checking for SET");
                var set = cmds.SingleOrDefault(cmd => cmd.Command == "SET");
                Assert.NotNull(set);
                Log("Checking for GET");
                var get = cmds.SingleOrDefault(cmd => cmd.Command == "GET");
                Assert.NotNull(get);
                Log("Checking for EVAL");
                var eval = cmds.SingleOrDefault(cmd => cmd.Command == "EVAL");
                Assert.NotNull(eval);
                Log("Checking for EVALSHA");
                var evalSha = cmds.SingleOrDefault(cmd => cmd.Command == "EVALSHA");
                Assert.NotNull(evalSha);
                Log("Checking for ECHO");
                var echo = cmds.SingleOrDefault(cmd => cmd.Command == "ECHO");
                Assert.NotNull(echo);

                Assert.Equal(5, cmds.Count());

                Assert.True(set.CommandCreated <= eval.CommandCreated);
                Assert.True(eval.CommandCreated <= evalSha.CommandCreated);
                Assert.True(evalSha.CommandCreated <= get.CommandCreated);

                AssertProfiledCommandValues(set, conn, dbId);

                AssertProfiledCommandValues(get, conn, dbId);

                AssertProfiledCommandValues(eval, conn, dbId);

                AssertProfiledCommandValues(evalSha, conn, dbId);

                AssertProfiledCommandValues(echo, conn, dbId);
            }
        }