Exemple #1
0
        /// <summary>
        /// Evaluates the expression and returns the value.
        /// </summary>
        /// <param name="webView"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public string EvaluateScript(BaseWebView webView, string expression) {
            latch = new CountDownLatch(1);
            string code = "javascript:window." + Name + ".setValue((function(){try{return " + expression
            + "+\"\";}catch(js_eval_err){console.log('js_eval_err : ' + js_eval_err); return '';}})());";
            webView.LoadUrl(code);

            try {
                // Set a 1 second timeout in case there's an error
                latch.Await(1, TimeUnit.Seconds);
                return returnValue;
            } catch (System.Exception ex) {
                DLogger.WriteLog(ex);
            }
            return null;
        }
Exemple #2
0
        /// <exception cref="System.IO.IOException"></exception>
        private void AddDocWithId(string docId, string attachmentName)
        {
            string docJson;

            if (attachmentName != null)
            {
                // add attachment to document
                var attachmentStream = (InputStream)GetAsset(attachmentName);
                var baos             = new MemoryStream();
                attachmentStream.Wrapped.CopyTo(baos);
                var attachmentBase64 = Convert.ToBase64String(baos.ToArray());
                docJson = String.Format("{{\"foo\":1,\"bar\":false, \"_attachments\": {{ \"i_use_couchdb.png\": {{ \"content_type\": \"image/png\", \"data\": \"{0}\" }} }} }}", attachmentBase64);
            }
            else
            {
                docJson = @"{""foo"":1,""bar"":false}";
            }

            // push a document to server
            var replicationUrlTrailingDoc1 = new Uri(string.Format("{0}/{1}", GetReplicationURL(), docId));
            var pathToDoc1 = new Uri(replicationUrlTrailingDoc1, docId);

            Log.D(Tag, "Send http request to " + pathToDoc1);
            CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);

            Task.Factory.StartNew(() =>
            {
                var httpclient = new HttpClient(); //CouchbaseLiteHttpClientFactory.Instance.GetHttpClient();
                HttpResponseMessage response;

                try
                {
                    var request = new HttpRequestMessage();
                    request.Headers.Add("Accept", "*/*");

                    var postTask   = httpclient.PutAsync(pathToDoc1.AbsoluteUri, new StringContent(docJson, Encoding.UTF8, "application/json"));
                    response       = postTask.Result;
                    var statusLine = response.StatusCode;
                    Log.D(ReplicationTest.Tag, "Got response: " + statusLine);
                    Assert.IsTrue(statusLine == HttpStatusCode.Created);
                }
                catch (ProtocolViolationException e)
                {
                    Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
                }
                catch (IOException e)
                {
                    Assert.IsNull(e, "Got IOException: " + e.Message);
                }

                httpRequestDoneSignal.CountDown();
            });

            Log.D(Tag, "Waiting for http request to finish");
            try
            {
                Assert.IsTrue(httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10)));
                Log.D(Tag, "http request finished");
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }

            WorkaroundSyncGatewayRaceCondition();
        }
Exemple #3
0
        public void TestPusherDeletedDoc()
        {
            var remote         = GetReplicationURL();
            var docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis());

            // Create some documentsConvert
            var documentProperties = new Dictionary <string, object>();
            var doc1Id             = string.Format("doc1-{0}", docIdTimestamp);

            documentProperties["_id"] = doc1Id;
            documentProperties["foo"] = 1;
            documentProperties["bar"] = false;

            var body   = new Body(documentProperties);
            var rev1   = new RevisionInternal(body, database);
            var status = new Status();

            rev1 = database.PutRevision(rev1, null, false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            documentProperties["_rev"]     = rev1.GetRevId();
            documentProperties["UPDATED"]  = true;
            documentProperties["_deleted"] = true;
            database.PutRevision(new RevisionInternal(documentProperties, database), rev1.GetRevId(), false, status);
            Assert.IsTrue((int)status.GetCode() >= 200 && (int)status.GetCode() < 300);

            var repl = database.CreatePushReplication(remote);

            if (!IsSyncGateway(remote))
            {
                ((Pusher)repl).CreateTarget = true;
            }

            RunReplication(repl);

            Assert.IsNull(repl.LastError);

            // make sure doc1 is deleted
            var replicationUrlTrailing = new Uri(string.Format("{0}/", remote));
            var pathToDoc = new Uri(replicationUrlTrailing, doc1Id);

            Log.D(Tag, "Send http request to " + pathToDoc);
            var httpRequestDoneSignal = new CountDownLatch(1);
            var httpclient            = new HttpClient();

            try
            {
                var getDocResponse = httpclient.GetAsync(pathToDoc.ToString()).Result;
                var statusLine     = getDocResponse.StatusCode;
                Log.D(ReplicationTest.Tag, "statusLine " + statusLine);
                Assert.AreEqual(Couchbase.Lite.StatusCode.NotFound, statusLine.GetStatusCode());
            }
            catch (ProtocolViolationException e)
            {
                Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
            }
            catch (IOException e)
            {
                Assert.IsNull(e, "Got IOException: " + e.Message);
            }
            finally
            {
                httpRequestDoneSignal.CountDown();
            }
            Log.D(Tag, "Waiting for http request to finish");
            try
            {
                httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10));
                Log.D(Tag, "http request finished");
            }
            catch (Exception e)
            {
                Runtime.PrintStackTrace(e);
            }
            Log.D(Tag, "testPusherDeletedDoc() finished");
        }
Exemple #4
0
        public void TestPushReplicationCanMissDocs()
        {
            Assert.AreEqual(0, database.LastSequenceNumber);

            var properties1 = new Dictionary <string, object>();

            properties1["doc1"] = "testPushReplicationCanMissDocs";
            var doc1 = CreateDocumentWithProperties(database, properties1);

            var properties2 = new Dictionary <string, object>();

            properties2["doc2"] = "testPushReplicationCanMissDocs";
            var doc2 = CreateDocumentWithProperties(database, properties2);

            var doc2UnsavedRev   = doc2.CreateRevision();
            var attachmentStream = GetAsset("attachment.png");

            doc2UnsavedRev.SetAttachment("attachment.png", "image/png", attachmentStream);
            var doc2Rev = doc2UnsavedRev.Save();

            Assert.IsNotNull(doc2Rev);

            var httpClientFactory = new MockHttpClientFactory();

            manager.DefaultHttpClientFactory = httpClientFactory;

            var httpHandler = httpClientFactory.HttpHandler;

            httpHandler.AddResponderFakeLocalDocumentUpdate404();

            var json = "{\"error\":\"not_found\",\"reason\":\"missing\"}";

            MockHttpRequestHandler.HttpResponseDelegate bulkDocsResponder = (request) =>
            {
                return(MockHttpRequestHandler.GenerateHttpResponseMessage(HttpStatusCode.NotFound, null, json));
            };
            httpHandler.SetResponder("_bulk_docs", bulkDocsResponder);

            MockHttpRequestHandler.HttpResponseDelegate doc2Responder = (request) =>
            {
                var responseObject = new Dictionary <string, object>();
                responseObject["id"]  = doc2.Id;
                responseObject["ok"]  = true;
                responseObject["rev"] = doc2.CurrentRevisionId;
                return(MockHttpRequestHandler.GenerateHttpResponseMessage(responseObject));
            };
            httpHandler.SetResponder(doc2.Id, bulkDocsResponder);

            var replicationDoneSignal = new CountDownLatch(1);
            var observer = new ReplicationObserver(replicationDoneSignal);
            var pusher   = database.CreatePushReplication(GetReplicationURL());

            pusher.Changed += observer.Changed;
            pusher.Start();

            var success = replicationDoneSignal.Await(TimeSpan.FromSeconds(5));

            Assert.IsTrue(success);

            Assert.IsNotNull(pusher.LastError);

            System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(500));

            var localLastSequence = database.LastSequenceWithCheckpointId(pusher.RemoteCheckpointDocID());

            Log.D(Tag, "dtabase.lastSequenceWithCheckpointId(): " + localLastSequence);
            Log.D(Tag, "doc2.getCUrrentRevision().getSequence(): " + doc2.CurrentRevision.Sequence);

            // Since doc1 failed, the database should _not_ have had its lastSequence bumped to doc2's sequence number.
            // If it did, it's bug: github.com/couchbase/couchbase-lite-java-core/issues/95
            Assert.IsFalse(doc2.CurrentRevision.Sequence.ToString().Equals(localLastSequence));
            Assert.IsNull(localLastSequence);
            Assert.IsTrue(doc2.CurrentRevision.Sequence > 0);
        }
        /// <exception cref="System.Exception"></exception>
        public virtual void TestPusher()
        {
            CountDownLatch replicationDoneSignal = new CountDownLatch(1);
            Uri            remote         = GetReplicationURL();
            string         docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis());
            // Create some documents:
            IDictionary <string, object> documentProperties = new Dictionary <string, object>();
            string doc1Id = string.Format("doc1-%s", docIdTimestamp);

            documentProperties.Put("_id", doc1Id);
            documentProperties.Put("foo", 1);
            documentProperties.Put("bar", false);
            Body             body   = new Body(documentProperties);
            RevisionInternal rev1   = new RevisionInternal(body, database);
            Status           status = new Status();

            rev1 = database.PutRevision(rev1, null, false, status);
            NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
            documentProperties.Put("_rev", rev1.GetRevId());
            documentProperties.Put("UPDATED", true);
            RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties
                                                                              , database), rev1.GetRevId(), false, status);

            NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
            documentProperties = new Dictionary <string, object>();
            string doc2Id = string.Format("doc2-%s", docIdTimestamp);

            documentProperties.Put("_id", doc2Id);
            documentProperties.Put("baz", 666);
            documentProperties.Put("fnord", true);
            database.PutRevision(new RevisionInternal(documentProperties, database), null, false
                                 , status);
            NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
            bool        continuous = false;
            Replication repl       = database.CreatePushReplication(remote);

            repl.SetContinuous(continuous);
            repl.SetCreateTarget(true);
            // Check the replication's properties:
            NUnit.Framework.Assert.AreEqual(database, repl.GetLocalDatabase());
            NUnit.Framework.Assert.AreEqual(remote, repl.GetRemoteUrl());
            NUnit.Framework.Assert.IsFalse(repl.IsPull());
            NUnit.Framework.Assert.IsFalse(repl.IsContinuous());
            NUnit.Framework.Assert.IsTrue(repl.ShouldCreateTarget());
            NUnit.Framework.Assert.IsNull(repl.GetFilter());
            NUnit.Framework.Assert.IsNull(repl.GetFilterParams());
            // TODO: CAssertNil(r1.doc_ids);
            // TODO: CAssertNil(r1.headers);
            // Check that the replication hasn't started running:
            NUnit.Framework.Assert.IsFalse(repl.IsRunning());
            NUnit.Framework.Assert.AreEqual(Replication.ReplicationStatus.ReplicationStopped,
                                            repl.GetStatus());
            NUnit.Framework.Assert.AreEqual(0, repl.GetCompletedChangesCount());
            NUnit.Framework.Assert.AreEqual(0, repl.GetChangesCount());
            NUnit.Framework.Assert.IsNull(repl.GetLastError());
            RunReplication(repl);
            // make sure doc1 is there
            // TODO: make sure doc2 is there (refactoring needed)
            Uri replicationUrlTrailing = new Uri(string.Format("%s/", remote.ToExternalForm()
                                                               ));
            Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id);

            Log.D(Tag, "Send http request to " + pathToDoc);
            CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
            BackgroundTask getDocTask            = new _BackgroundTask_122(pathToDoc, doc1Id, httpRequestDoneSignal
                                                                           );

            //Closes the connection.
            getDocTask.Execute();
            Log.D(Tag, "Waiting for http request to finish");
            try
            {
                httpRequestDoneSignal.Await(300, TimeUnit.Seconds);
                Log.D(Tag, "http request finished");
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }
            Log.D(Tag, "testPusher() finished");
        }
//        public static Task CreateDocumentsAsync(Database db, int n)
//      {
//            return db.RunAsync((database)=>
//                {
//                    database.BeginTransaction();
//                    ApiTest.CreateDocuments(database, n);
//                    database.EndTransaction(true);
//                });
//      }

//          public static void CreateDocuments(Database db, int numberOfDocsToCreate)
//      {
//          //TODO should be changed to use db.runInTransaction
//          for (int i = 0; i < numberOfDocsToCreate; i++)
//          {
//                var properties = new Dictionary<String, Object>();
//                properties["testName"] = "testDatabase";
//                properties["sequence"] = i;
//              CreateDocumentWithProperties(db, properties);
//          }
//      }
//
//        public static Document CreateDocumentWithProperties(Database db, IDictionary<String, Object> properties)
//      {
//            var doc = db.CreateDocument();
//
//          Assert.IsNotNull(doc);
//          Assert.IsNull(doc.CurrentRevisionId);
//          Assert.IsNull(doc.CurrentRevision);
//          Assert.IsNotNull("Document has no ID", doc.Id);
//
//          // 'untitled' docs are no longer untitled (8/10/12)
//          try
//          {
//              doc.PutProperties(properties);
//          }
//          catch (Exception e)
//          {
//              Log.E(Tag, "Error creating document", e);
//                Assert.IsTrue( false, "can't create new document in db:" + db.Name +
//                    " with properties:" + properties.Aggregate(new StringBuilder(" >>> "), (str, kvp)=> { str.AppendFormat("'{0}:{1}' ", kvp.Key, kvp.Value); return str; }, str=>str.ToString()));
//          }
//
//          Assert.IsNotNull(doc.Id);
//          Assert.IsNotNull(doc.CurrentRevisionId);
//          Assert.IsNotNull(doc.UserProperties);
//          Assert.AreEqual(db.GetDocument(doc.Id), doc);
//
//          return doc;
//      }

        /// <exception cref="System.Exception"></exception>
        public void RunLiveQuery(String methodNameToCall)
        {
            var db = StartDatabase();

            var doneSignal = new CountDownLatch(11); // FIXME.ZJG: Not sure why, but now Changed is only called once.

            // 11 corresponds to startKey = 23; endKey = 33
            // run a live query
            var view = db.GetView("vu");

            view.SetMap((document, emitter) => emitter(document ["sequence"], 1), "1");

            var query = view.CreateQuery().ToLiveQuery();

            query.StartKey = 23;
            query.EndKey   = 33;

            Log.I(Tag, "Created  " + query);

            // these are the keys that we expect to see in the livequery change listener callback
            var expectedKeys = new HashSet <Int64>();

            for (var i = 23; i < 34; i++)
            {
                expectedKeys.AddItem(i);
            }

            // install a change listener which decrements countdown latch when it sees a new
            // key from the list of expected keys
            EventHandler <QueryChangeEventArgs> handler = (sender, e) => {
                var rows = e.Rows;
                foreach (var row in rows)
                {
                    if (expectedKeys.Contains((Int64)row.Key))
                    {
                        Log.I(Tag, " doneSignal decremented " + doneSignal.Count);
                        doneSignal.CountDown();
                    }
                }
            };

            query.Changed += handler;

            // create the docs that will cause the above change listener to decrement countdown latch
            var createTask = CreateDocumentsAsync(db, n: 50);

            createTask.Wait(TimeSpan.FromSeconds(5));
            if (methodNameToCall.Equals("start"))
            {
                // start the livequery running asynchronously
                query.Start();
            }
            else
            {
                Assert.IsNull(query.Rows);

                query.Run();

                // this will block until the query completes
                Assert.IsNotNull(query.Rows);
            }

            // wait for the doneSignal to be finished
            var success = doneSignal.Await(TimeSpan.FromSeconds(5));

            Assert.IsTrue(success, "Done signal timed out live query never ran");

            // stop the livequery since we are done with it
            query.Changed -= handler;
            query.Stop();
        }
Exemple #7
0
        public void test_PropertyChange_OutOfGuiThread()
        {
            HashMap <String, IMap <Thread, int> > counter = new HashMap <String, IMap <Thread, int> >();
            PropertyChangedEventHandler           handler = GetPropertyChangeHandlerForUI(counter);

            Thread         workerThread = null;
            CountDownLatch latch        = new CountDownLatch(1);

            ThreadPool.Queue(delegate()
            {
                workerThread = Thread.CurrentThread;
                Log.Info("Test()");
                try
                {
                    Material obj = EntityFactory.CreateEntity <Material>();
                    ((INotifyPropertyChanged)obj).PropertyChanged += handler;

                    Log.Info("ICacheModification.set_Active(true)");
                    CacheModification.Active = true;
                    try
                    {
                        Log.Info("set_Id");
                        obj.Id = 1;
                        Log.Info("set_Id finished");
                        Assert.AssertEquals(0, counter.Count);
                    }
                    finally
                    {
                        Log.Info("ICacheModification.set_Active(false)");
                        CacheModification.Active = false;
                        Log.Info("ICacheModification.set_Active(false) finished");
                    }
                    WaitForUI();
                    Assert.AssertEquals(3, counter.Count);
                    Log.Info(" set_Name");
                    obj.Name = "hallo";
                    WaitForUI();
                    Log.Info("set_Name finished");
                    Assert.AssertEquals(5, counter.Count);
                }
                finally
                {
                    latch.CountDown();
                }
            });
            Log.Info("Await()");
            latch.Await();
            // Wait till the current ui queue has been processed completely
            GuiThreadHelper.InvokeInGuiAndWait(delegate()
            {
                // just an empty blocking delegate
            });
            Assert.AssertEquals(5, counter.Count);

            Thread guiThread = ValueHolderContainerTestModule.dispatcherThread;

            IMap <Thread, int> toBeCreatedMap = counter.Get("ToBeCreated");

            Assert.AssertNotNull(toBeCreatedMap);
            Assert.AssertEquals(1, toBeCreatedMap.Count);
            Assert.AssertTrue(toBeCreatedMap.ContainsKey(guiThread));
            Assert.AssertEquals(1, toBeCreatedMap.Get(guiThread));

            IMap <Thread, int> idMap = counter.Get("Id");

            Assert.AssertNotNull(idMap);
            Assert.AssertEquals(1, idMap.Count);
            Assert.AssertTrue(idMap.ContainsKey(guiThread));
            Assert.AssertEquals(1, idMap.Get(guiThread));

            // uiThread is intended for Name in the case where asynchronous PCEs are allowed
            // but dispatched transparently in the UI
            IMap <Thread, int> nameMap = counter.Get("Name");

            Assert.AssertNotNull(nameMap);
            Assert.AssertEquals(1, nameMap.Count);
            Assert.AssertTrue(idMap.ContainsKey(guiThread));
            Assert.AssertEquals(1, nameMap.Get(guiThread));

            IMap <Thread, int> toBeUpdatedMap = counter.Get("ToBeUpdated");

            Assert.AssertNotNull(toBeUpdatedMap);
            Assert.AssertEquals(1, toBeUpdatedMap.Count);
            Assert.AssertTrue(toBeUpdatedMap.ContainsKey(guiThread));
            Assert.AssertEquals(1, toBeUpdatedMap.Get(guiThread));

            IMap <Thread, int> hasPendingChangesMap = counter.Get("HasPendingChanges");

            Assert.AssertNotNull(hasPendingChangesMap);
            Assert.AssertEquals(1, hasPendingChangesMap.Count);
            Assert.AssertTrue(hasPendingChangesMap.ContainsKey(guiThread));
            Assert.AssertEquals(2, hasPendingChangesMap.Get(guiThread));
        }
 /// <summary>
 /// After calling proceed(), this will wait until the call has
 /// completed and a result has been returned to the caller.
 /// </summary>
 /// <exception cref="System.Exception"/>
 public virtual void WaitForResult()
 {
     resultLatch.Await();
 }
Exemple #9
0
        /// <exception cref="System.Exception"/>
        public virtual void TestKillJob()
        {
            JobConf    conf    = new JobConf();
            AppContext context = Org.Mockito.Mockito.Mock <AppContext>();
            // a simple event handler solely to detect the container cleaned event
            CountDownLatch isDone  = new CountDownLatch(1);
            EventHandler   handler = new _EventHandler_106(isDone);

            Org.Mockito.Mockito.When(context.GetEventHandler()).ThenReturn(handler);
            // create and start the launcher
            LocalContainerLauncher launcher = new LocalContainerLauncher(context, Org.Mockito.Mockito.Mock
                                                                         <TaskUmbilicalProtocol>());

            launcher.Init(conf);
            launcher.Start();
            // create mocked job, task, and task attempt
            // a single-mapper job
            JobId         jobId  = MRBuilderUtils.NewJobId(Runtime.CurrentTimeMillis(), 1, 1);
            TaskId        taskId = MRBuilderUtils.NewTaskId(jobId, 1, TaskType.Map);
            TaskAttemptId taId   = MRBuilderUtils.NewTaskAttemptId(taskId, 0);

            Org.Apache.Hadoop.Mapreduce.V2.App.Job.Job job = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Mapreduce.V2.App.Job.Job
                                                                                       >();
            Org.Mockito.Mockito.When(job.GetTotalMaps()).ThenReturn(1);
            Org.Mockito.Mockito.When(job.GetTotalReduces()).ThenReturn(0);
            IDictionary <JobId, Org.Apache.Hadoop.Mapreduce.V2.App.Job.Job> jobs = new Dictionary
                                                                                   <JobId, Org.Apache.Hadoop.Mapreduce.V2.App.Job.Job>();

            jobs[jobId] = job;
            // app context returns the one and only job
            Org.Mockito.Mockito.When(context.GetAllJobs()).ThenReturn(jobs);
            Task ytask = Org.Mockito.Mockito.Mock <Task>();

            Org.Mockito.Mockito.When(ytask.GetType()).ThenReturn(TaskType.Map);
            Org.Mockito.Mockito.When(job.GetTask(taskId)).ThenReturn(ytask);
            // create a sleeping mapper that runs beyond the test timeout
            MapTask mapTask = Org.Mockito.Mockito.Mock <MapTask>();

            Org.Mockito.Mockito.When(mapTask.IsMapOrReduce()).ThenReturn(true);
            Org.Mockito.Mockito.When(mapTask.IsMapTask()).ThenReturn(true);
            TaskAttemptID taskID = TypeConverter.FromYarn(taId);

            Org.Mockito.Mockito.When(mapTask.GetTaskID()).ThenReturn(taskID);
            Org.Mockito.Mockito.When(mapTask.GetJobID()).ThenReturn(((JobID)taskID.GetJobID()
                                                                     ));
            Org.Mockito.Mockito.DoAnswer(new _Answer_152()).When(mapTask).Run(Matchers.IsA <JobConf
                                                                                            >(), Matchers.IsA <TaskUmbilicalProtocol>());
            // sleep for a long time
            // pump in a task attempt launch event
            ContainerLauncherEvent launchEvent = new ContainerRemoteLaunchEvent(taId, null, CreateMockContainer
                                                                                    (), mapTask);

            launcher.Handle(launchEvent);
            Sharpen.Thread.Sleep(200);
            // now pump in a container clean-up event
            ContainerLauncherEvent cleanupEvent = new ContainerLauncherEvent(taId, null, null
                                                                             , null, ContainerLauncher.EventType.ContainerRemoteCleanup);

            launcher.Handle(cleanupEvent);
            // wait for the event to fire: this should be received promptly
            isDone.Await();
            launcher.Close();
        }
Exemple #10
0
        /// <summary>Test if Network as a whole has terminated
        /// </summary>

        internal virtual void WaitForAll()
        {
            bool possibleDeadlock = false;
            //bool deadlock = false;

            int freq = 500;   // check every .5 second

            //Settings s = Settings.Default;
            while (true)
            {
                if (_cdl.Await(new TimeSpan(0, 0, 0, 0, freq))) // 500 msecs
                {
                    break;                                      // if CountDownLatch finished, exit
                }
                // if an error occurred, skip deadlock testing
                if (_error != null)
                {
                    break;
                }

                // if the network was aborted, skip deadlock testing
                if (_abort)
                {
                    break;
                }
                if (!_deadlockTest)
                {
                    continue;
                }
                if (stgs.DeadlockTestEnabled)
                {
                    TestTimeouts(freq);
                    if (!_active)  // else interval elapsed
                    {
                        if (!possibleDeadlock)
                        {
                            possibleDeadlock = true;
                        }
                        else
                        {
                            _deadlock = true;
                            // well, maybe
                            // so test state of components
                            _msgs = new List <String>();
                            _msgs.Add("Network has deadlocked");
                            if (ListCompStatus(_msgs))
                            {
                                //          interruptAll();
                                foreach (string m in _msgs)
                                {
                                    Console.Out.WriteLine(m);
                                }
                                Console.Out.WriteLine("*** Deadlock detected in Network ");
                                Console.Out.Flush();

                                // terminate the net instead of crashing the application
                                Terminate();
                                // tell the caller a deadlock occurred
                                FlowError.Complain("Deadlock detected in Network");
                                break;
                            }
                            // one or more components haven't started or
                            // are in a long wait
                            _deadlock        = false;
                            possibleDeadlock = false;
                        }
                    }
                    _active = false;
                }
            }

            if (_deadlock)
            {
                InterruptAll();
            }
            else
            {
                foreach (Component c in _components.Values)
                {
                    if (c._thread != null)
                    {
                        c._thread.Join();
                    }
                }

                //if (outputCopier != null)
                //   outputCopier.Join();
            }
        }
Exemple #11
0
        public void AfterStarted()
        {
            DirectoryInfo targetInfo = new DirectoryInfo(TargetDir);

            targetInfo.Create();

            Log.Info("Saving all generated images to '" + targetInfo.FullName + "'");

            String[] sources = SourceDir.Split(';');
            foreach (String source in sources)
            {
                Log.Info("Scanning for office files in '" + source + "'");
                DirectoryInfo di          = new DirectoryInfo(source);
                FileInfo[]    directories = di.GetFiles("*", SearchOption.AllDirectories);

                IdentityHashMap <IFileParser, List <FileInfo> > queuedFilesMap = new IdentityHashMap <IFileParser, List <FileInfo> >();

                foreach (FileInfo sourceFile in directories)
                {
                    if (sourceFile.Name.Contains('~') || !sourceFile.Exists)
                    {
                        continue;
                    }
                    String lowercaseExtensionName = sourceFile.Extension.ToLowerInvariant();
                    if (lowercaseExtensionName.StartsWith("."))
                    {
                        lowercaseExtensionName = lowercaseExtensionName.Substring(1);
                    }
                    IFileParser fileParser = fileParsers.GetExtension(lowercaseExtensionName);
                    if (fileParser == null)
                    {
                        Log.Debug("Skipping '" + sourceFile.FullName + "': no parser configured for '" + lowercaseExtensionName + "'");
                        continue;
                    }
                    List <FileInfo> queuedFiles = queuedFilesMap.Get(fileParser);
                    if (queuedFiles == null)
                    {
                        queuedFiles = new List <FileInfo>();
                        queuedFilesMap.Put(fileParser, queuedFiles);
                    }
                    queuedFiles.Add(sourceFile);
                }
                List <Thread>  threads = new List <Thread>();
                CountDownLatch latch   = new CountDownLatch(queuedFilesMap.Count);

                foreach (Entry <IFileParser, List <FileInfo> > entry in queuedFilesMap)
                {
                    IFileParser      fileParser  = entry.Key;
                    IList <FileInfo> sourceFiles = entry.Value;
                    Thread           thread      = new Thread(new ThreadStart(delegate()
                    {
                        ParseFiles(fileParser, sourceFiles, targetInfo, latch);
                    }));
                    thread.Name         = fileParser.GetType().Name;
                    thread.IsBackground = true;
                    threads.Add(thread);
                }
                foreach (Thread thread in threads)
                {
                    thread.Start();
                }
                latch.Await(TimeSpan.FromMinutes(5)); // throw exception after some minutes
            }
        }
Exemple #12
0
        public void WaitEventToResume(Object eventTargetToResume, long maxWaitTime, IBackgroundWorkerParamDelegate <IProcessResumeItem> resumeDelegate, IBackgroundWorkerParamDelegate <Exception> errorDelegate)
        {
            try
            {
                IdentityHashSet <Object> pendingSet = new IdentityHashSet <Object>();
                if (eventTargetToResume is IEnumerable)
                {
                    pendingSet.AddAll((IEnumerable)eventTargetToResume);
                }
                else
                {
                    pendingSet.Add(eventTargetToResume);
                }
                WaitForResumeItem pauseItem = null;
                listenersWriteLock.Lock();
                try
                {
                    IList <Object>             remainingPausedEventTargets    = EvaluatePausedEventTargetsOfForeignThreads();
                    IdentityLinkedSet <Object> remainingPausedEventTargetsSet = new IdentityLinkedSet <Object>(remainingPausedEventTargets);
                    remainingPausedEventTargetsSet.RetainAll(pendingSet);

                    if (remainingPausedEventTargetsSet.Count > 0)
                    {
                        // We should wait now but we have to check if we are in the UI thread, which must never wait
                        if (GuiThreadHelper.IsInGuiThread())
                        {
                            // This is the trick: We "requeue" the current action in the UI pipeline to prohibit blocking
                            GuiThreadHelper.InvokeInGuiLate(delegate()
                            {
                                WaitEventToResume(eventTargetToResume, maxWaitTime, resumeDelegate, errorDelegate);
                            });
                            return;
                        }
                        pauseItem = new WaitForResumeItem(pendingSet);
                        waitForResumeSet.Add(pauseItem);
                    }
                }
                finally
                {
                    listenersWriteLock.Unlock();
                }
                if (pauseItem == null)
                {
                    resumeDelegate.Invoke(null);
                    return;
                }
                CountDownLatch latch = pauseItem.Latch;
                if (maxWaitTime < 0)
                {
                    latch.Await();
                }
                else if (maxWaitTime > 0)
                {
                    latch.Await(TimeSpan.FromMilliseconds(maxWaitTime));
                }
                else
                {
                    throw new System.Exception("Thread should wait but does not want to");
                }
                resumeDelegate.Invoke(pauseItem);
            }
            catch (Exception e)
            {
                if (Log.ErrorEnabled)
                {
                    Log.Error(e);
                }
                if (errorDelegate != null)
                {
                    errorDelegate.Invoke(e);
                }
                throw;
            }
        }
Exemple #13
0
 public T Get()
 {
     _latch.Await();
     Debug.Assert(!Equals(_result, default(T)));
     return(_result);
 }
        public void Test30LiveQuery()
        {
            RunTest("Test30LiveQuery", (parameters) =>
            {
                var numDocs = Convert.ToInt32(parameters[NUMDOCS_KEY]);
                var docSize = Convert.ToInt32(parameters[DOCSIZE_KEY]);

                // Prepare document content
                var sb = new StringBuilder();
                for (var s = 0; s < docSize; s++)
                {
                    sb.Append("1");
                }
                var name = sb.ToString();

                var props     = new Dictionary <string, object>();
                props["name"] = name;

                var doneSignal = new CountDownLatch(1);

                // Run a live query
                var view = database.GetView("vu");
                view.SetMap((document, emit) =>
                {
                    emit(document["sequence"], null);
                }, "1");

                var liveQuery      = view.CreateQuery().ToLiveQuery();
                liveQuery.Changed += (sender, e) =>
                {
                    var rows  = e.Rows;
                    var count = rows.Count;
                    if (count == numDocs)
                    {
                        doneSignal.CountDown();
                    }
                };
                liveQuery.Start();

                var stopwatch = Stopwatch.StartNew();

                database.RunAsync((db) =>
                {
                    database.BeginTransaction();

                    for (var i = 0; i < numDocs; i++)
                    {
                        var doc           = database.CreateDocument();
                        props["sequence"] = i;
                        var rev           = doc.PutProperties(props);
                        Assert.IsNotNull(rev);
                    }

                    db.EndTransaction(true);
                });

                var success = doneSignal.Await(TimeSpan.FromSeconds(300));
                Assert.IsTrue(success);

                stopwatch.Stop();

                return(stopwatch.ElapsedMilliseconds);
            });
        }
Exemple #15
0
 public override bool WaitOne(TimeSpan timeout, bool exitContext)
 {
     return(_latch.Await(timeout));
 }
        public virtual void TestPusherDeletedDoc()
        {
            Assert.Fail(); // TODO.ZJG: Needs debugging, overflows stack.

            CountDownLatch replicationDoneSignal = new CountDownLatch(1);
            Uri            remote         = GetReplicationURL();
            string         docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis());
            // Create some documentsConvert
            IDictionary <string, object> documentProperties = new Dictionary <string, object>();
            string doc1Id = string.Format("doc1-{0}", docIdTimestamp);

            documentProperties["_id"] = doc1Id;
            documentProperties["foo"] = 1;
            documentProperties["bar"] = false;
            Body             body   = new Body(documentProperties);
            RevisionInternal rev1   = new RevisionInternal(body, database);
            Status           status = new Status();

            rev1 = database.PutRevision(rev1, null, false, status);
            NUnit.Framework.Assert.AreEqual(StatusCode.Created, status.GetCode());
            documentProperties["_rev"]     = rev1.GetRevId();
            documentProperties["UPDATED"]  = true;
            documentProperties["_deleted"] = true;
            RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties
                                                                              , database), rev1.GetRevId(), false, status);

            NUnit.Framework.Assert.IsTrue((int)status.GetCode() >= 200 && (int)status.GetCode() < 300);
            var repl = database.CreatePushReplication(remote);

            ((Pusher)repl).CreateTarget = true;
            RunReplication(repl);
            // make sure doc1 is deleted
            Uri replicationUrlTrailing = new Uri(string.Format("{0}/", remote.ToString()
                                                               ));
            Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id);

            Log.D(Tag, "Send http request to " + pathToDoc);
            CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
            var            getDocTask            = Task.Factory.StartNew(() =>
            {
                var httpclient = new HttpClient();
                HttpResponseMessage response;
                string responseString = null;
                try
                {
                    var responseTask = httpclient.GetAsync(pathToDoc.ToString());
                    responseTask.Wait();
                    response       = responseTask.Result;
                    var statusLine = response.StatusCode;
                    Log.D(ReplicationTest.Tag, "statusLine " + statusLine);
                    Assert.AreEqual(HttpStatusCode.NotFound, statusLine.GetStatusCode());
                }
                catch (ProtocolViolationException e)
                {
                    NUnit.Framework.Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
                }
                catch (IOException e)
                {
                    NUnit.Framework.Assert.IsNull(e, "Got IOException: " + e.Message);
                }
                finally
                {
                    httpRequestDoneSignal.CountDown();
                }
            });

            getDocTask.Start();
            Log.D(Tag, "Waiting for http request to finish");
            try
            {
                httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10));
                Log.D(Tag, "http request finished");
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }
            Log.D(Tag, "testPusherDeletedDoc() finished");
        }
 /// <summary>Wait until the method is called.</summary>
 /// <exception cref="System.Exception"/>
 public virtual void WaitForCall()
 {
     fireLatch.Await();
 }