Exemple #1
0
 public InnerVerifier(string testName, string sourceFile, Assembly assembly)
 {
     directory     = VerifierSettings.DeriveDirectory(sourceFile, assembly);
     this.testName = testName;
     this.assembly = assembly;
     CounterContext.Start();
 }
Exemple #2
0
        public void ConcurrentWaitForLock()
        {
            var NTHREADS       = 1;
            var signal         = new ManualResetEvent(false);
            var counterContext = new CounterContext()
            {
                Counter = 0
            };
            var contexts = Enumerable.Repeat <Func <ConcurrentContext> >(() => new ConcurrentContext(signal, counterContext, ConcurrentLock_LOOP, waitForLock: true), NTHREADS)
                           .Select(a => a()).ToList();

            var threads = contexts.Select(c => new Thread(c.ConcurrentLock_Start)).ToList();

            threads.ForEach(t => t.Start());

            //Runs all threads at the same time.
            contexts.ForEach(c => c.ThreadStartSignal.WaitOne());
            signal.Set();

            //Waits until all threads finish
            threads.ForEach(t => t.Join());

            //Each thread adds ConcurrentLock_LOOP to the counter. If everything is ok the sum should be (ConcurrentLock_LOOP * NTHREADS threads);

            Assert.Equal(ConcurrentLock_LOOP * NTHREADS, counterContext.Counter);
        }
        public int WaitForCounterToStabilize(CounterContext context, int timeout = 20000, int pollStep = 500)
        {
            int lastValue = context.InitialCount;

            Func <int, bool> isDone = current => {
                // Check if the UI thread is stuck
                // Some counters require UI thread synchronization, so we might not be getting events
                try {
                    ExecuteOnIdle(() => { }, timeout: 5000);
                } catch (TimeoutException) {
                    return(false);
                }

                // We're still getting value updates.
                if (current != lastValue)
                {
                    lastValue = current;
                    return(false);
                }

                return(true);
            };

            return(WaitForCounterToChange(context, isDone, timeout, pollStep));
        }
Exemple #4
0
 public ConcurrentContext(ManualResetEvent signal, CounterContext counterContext, int loopCount, bool waitForLock)
 {
     _counterContext    = counterContext;
     _signal            = signal;
     _waitForLock       = waitForLock;
     _loopCount         = loopCount;
     _ThreadStartSignal = new ManualResetEvent(false);
 }
Exemple #5
0
        public InnerVerifier(string sourceFile, Type type, VerifySettings settings, MethodInfo method, IReadOnlyList <object?>?parameters)
        {
            var(projectDirectory, replacements) = AttributeReader.GetAssemblyInfo(type.Assembly);
            settings.instanceScrubbers.Add(replacements);
            fileNameBuilder = new FileNameBuilder(method, type, projectDirectory, sourceFile, parameters, settings);

            this.settings = settings;

            CounterContext.Start();
        }
Exemple #6
0
        protected void Page_PreInit(object sender, EventArgs e)
        {
            this.context = new CounterContext();

            context.Logs.Add(new Log()
            {
                IpAddres     = this.GetIpAddress(),
                VisitingDate = DateTime.Now
            });

            context.SaveChanges();
        }
Exemple #7
0
        protected void Page_Load(object sender, EventArgs e)
        {
            CounterContext db    = new CounterContext();
            var            found = db.Visitors.FirstOrDefault();

            if (found == null)
            {
                db.Visitors.Add(new Visitor()
                {
                    Count = 1
                });
                db.SaveChanges();
                found = new Visitor()
                {
                    Count = 1
                };
            }
            else
            {
                found.Count++;
                db.SaveChanges();
            }


            Bitmap generatedImage = new Bitmap(110, 110);

            using (generatedImage)
            {
                Graphics gr = Graphics.FromImage(generatedImage);
                using (gr)
                {
                    gr.FillRectangle(Brushes.MediumSeaGreen, 0, 0, 200, 200);

                    gr.DrawString(found.Count.ToString(),
                                  new Font("Segoe UI", 25),
                                  new SolidBrush(Color.BlanchedAlmond),
                                  new PointF(35, 30));


                    // Set response header and write the image into response stream
                    Response.ContentType = "image/jpeg";

                    //Response.AppendHeader("Content-Disposition",
                    //    "attachment; filename=\"Financial-Report-April-2013.gif\"");

                    generatedImage.Save(Response.OutputStream, ImageFormat.Jpeg);
                }
            }
        }
Exemple #8
0
        public CounterContext CreateNewCounterContext(string counterName)
        {
            var counter = GetCounterByIDOrName(counterName);

            if (counter == null)
            {
                throw new Exception($"Unknown counter {counterName}");
            }

            var context = new CounterContext {
                CounterName  = counterName,
                InitialCount = counter.Count
            };

            return(context);
        }
Exemple #9
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env
                              , CounterContext context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            context.Database.Migrate();

            app.UseHttpsRedirection();
            app.UseMvc();
        }
Exemple #10
0
        public void WaitForCounterToChange(CounterContext context, int timeout = 20000, int pollStep = 200)
        {
            var counter = GetCounterByIDOrName(context.CounterName);

            if (counter == null)
            {
                throw new Exception($"Unknown counter {context.CounterName}");
            }

            do
            {
                if (counter.Count != context.InitialCount)
                {
                    return;
                }

                timeout -= pollStep;
                Thread.Sleep(pollStep);
            } while (timeout > 0);

            throw new TimeoutException("Timed out waiting for counter");
        }
        int WaitForCounterToChange(CounterContext context, Func <int, bool> isDone, int timeout = 20000, int pollStep = 200)
        {
            var counter = GetCounterByIDOrName(context.CounterName);

            if (counter == null)
            {
                throw new Exception($"Unknown counter {context.CounterName}");
            }

            do
            {
                if (isDone(counter.Count))
                {
                    return(counter.Count);
                }

                timeout -= pollStep;
                Thread.Sleep(pollStep);
            } while (timeout > 0);

            throw new TimeoutException($"Timed out waiting for counter {context.CounterName}");
        }
Exemple #12
0
        public InnerVerifier(string testName, string sourceFile, Assembly assembly, VerifySettings settings)
        {
            var projectDirectory = AttributeReader.GetProjectDirectory(assembly);

            directory     = VerifierSettings.DeriveDirectory(sourceFile, projectDirectory);
            this.testName = testName;
            this.assembly = assembly;
            this.settings = settings;

            var altProjectDirectory        = projectDirectory.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            var altProjectDirectoryTrimmed = altProjectDirectory.TrimEnd('/', '\\');
            var projectDirectoryTrimmed    = projectDirectory.TrimEnd('/', '\\');

            settings.instanceScrubbers.Add(builder =>
            {
                builder.Replace(projectDirectory, "{ProjectDirectory}");
                builder.Replace(projectDirectoryTrimmed, "{ProjectDirectory}");
                builder.Replace(altProjectDirectory, "{ProjectDirectory}");
                builder.Replace(altProjectDirectoryTrimmed, "{ProjectDirectory}");
            });

            if (AttributeReader.TryGetSolutionDirectory(assembly, out var solutionDirectory))
            {
                var altSolutionDirectory        = solutionDirectory !.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
                var altSolutionDirectoryTrimmed = altSolutionDirectory.TrimEnd('/', '\\');
                var solutionDirectoryTrimmed    = solutionDirectory.TrimEnd('/', '\\');
                settings.instanceScrubbers.Add(builder =>
                {
                    builder.Replace(solutionDirectory, "{SolutionDirectory}");
                    builder.Replace(solutionDirectoryTrimmed, "{SolutionDirectory}");
                    builder.Replace(altSolutionDirectory, "{SolutionDirectory}");
                    builder.Replace(altSolutionDirectoryTrimmed, "{SolutionDirectory}");
                });
            }


            CounterContext.Start();
        }
        public int UpdateCounter()
        {
            int defaultCounter = 1;

            try
            {
                using (var context = new CounterContext())
                {
                    if (context.Counters.Count() == 0)
                    {
                        // Add new counter if no exist
                        context.Counters.Add(new Counter {
                            ClickCount = defaultCounter
                        });
                    }
                    else
                    {
                        //update counter if exist
                        var counter = context.Counters.First();
                        if (counter.ClickCount >= 10)
                        {
                            return(counter.ClickCount);
                        }
                        counter.ClickCount = counter.ClickCount + defaultCounter;
                    }
                    context.SaveChanges();

                    // return number of click
                    return(context.Counters.First().ClickCount);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemple #14
0
 public InnerVerifier(string testName, string sourceFile)
 {
     directory     = SharedVerifySettings.DeriveDirectory(sourceFile);
     this.testName = testName;
     CounterContext.Start();
 }
Exemple #15
0
 public IndexModel(CounterContext db)
 {
     this.db = db;
 }
Exemple #16
0
 public DbRepository(CounterContext context)
 {
     _context = context;
 }
Exemple #17
0
        public override void Execute(string[] commandline)
        {
            if (commandline.Length < 2)
            {
                _console.Out.WriteLine("Error: [local_alias] not specified");
                return;
            }
            else if (commandline.Length < 3)
            {
                _console.Out.WriteLine("Error: [counter_subcommand] not specified");
                return;
            }

            ClientContext ctx = _console.GetValue <ClientContext> ("client_context", null);

            if (ctx == null)
            {
                _console.Out.WriteLine("No active connection was found");
                return;
            }

            string localAlias     = commandline[1];
            string counterCommand = commandline[2];

            IDictionary <string, TPMSession> tpmSessions = _console.GetValue <IDictionary <string, TPMSession> > ("tpm_sessions", null);

            if (tpmSessions == null || tpmSessions.ContainsKey(localAlias) == false)
            {
                _console.Out.WriteLine("Error: Specified local alias was not found");
                return;
            }



            if (counterCommand == "create")
            {
                //ProtectedPasswordStorage ownerAuth;
                if (commandline.Length < 4)
                {
                    _console.Out.WriteLine("Error: Could not find label");
                    return;
                }

                string sLabel = commandline[3];
                if (sLabel.Length != 4)
                {
                    _console.Out.WriteLine("Error: Label needs to be 4 chars long");
                    return;
                }

                CounterContext counterCtx = tpmSessions[localAlias].CounterClient.CreateCounter(Encoding.ASCII.GetBytes(sLabel));
                _console.Out.WriteLine("Created new counter with id #{0}", counterCtx.CounterId);
            }

            else if (counterCommand == "read")
            {
                //ProtectedPasswordStorage ownerAuth;
                if (commandline.Length < 4)
                {
                    _console.Out.WriteLine("Error: Could not find counter id");
                    return;
                }

                uint           counterId = uint.Parse(commandline[3]);
                CounterContext counter   = tpmSessions[localAlias].CounterClient.GetCounter(counterId);
                _console.Out.WriteLine("Value of counter #{0}: {1}", counter.CounterId, counter.CounterValue);
            }

            else if (counterCommand == "increment")
            {
                //ProtectedPasswordStorage ownerAuth;
                if (commandline.Length < 4)
                {
                    _console.Out.WriteLine("Error: Could not find counter id");
                    return;
                }

                uint           counterId = uint.Parse(commandline[3]);
                CounterContext counter   = tpmSessions[localAlias].CounterClient.GetCounter(counterId);
                _console.Out.WriteLine("Incremented counter, new value of counter #{0}: {1}", counter.CounterId, counter.Increment());
            }

            else if (counterCommand == "release")
            {
                //ProtectedPasswordStorage ownerAuth;
                if (commandline.Length < 4)
                {
                    _console.Out.WriteLine("Error: Could not find counter id");
                    return;
                }

                uint           counterId = uint.Parse(commandline[3]);
                CounterContext counter   = tpmSessions[localAlias].CounterClient.GetCounter(counterId);
                counter.Release();
                _console.Out.WriteLine("Released counter #{0}", counter.CounterId);
            }
            else
            {
                _console.Out.WriteLine("Error, unknown counter_subcommand '{0}'", counterCommand);
            }
        }
 public int WaitForCounterToExceed(CounterContext context, int count, int timeout = 20000, int pollStep = 500)
 => WaitForCounterToChange(context, current => current >= count, timeout, pollStep);
 public void WaitForCounterToChange(CounterContext context, int timeout = 20000, int pollStep = 200)
 => WaitForCounterToChange(context, current => current != context.InitialCount, timeout, pollStep);
 public RepositoryWrapper(CounterContext repositoryContext)
 {
     _repoContext = repositoryContext;
 }
 public RepositoryBase(CounterContext repositoryContext)
 {
     this.RepositoryContext = repositoryContext;
 }
Exemple #22
0
 public void Dispose()
 {
     CounterContext.Stop();
 }
Exemple #23
0
 public HomeController(CounterContext counterContext)
 {
     _context = counterContext;
 }
Exemple #24
0
 public DisposableVerifier(Type testType, string directory, string testName) :
     base(testType, directory, testName)
 {
     CounterContext.Start();
 }
Exemple #25
0
 public CounterRepository(CounterContext repositoryContext) : base(repositoryContext)
 {
 }
 public CountersController(CounterContext db)
 {
     _db = db;
 }