/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.vcs.changes;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;

/**
 * for non-AWT threads, synchronously waits for completion of ChanegListManager update
 */
public class EnsureUpToDateFromNonAWTThread {
  private final Project myProject;
  private volatile boolean myDone;

  public EnsureUpToDateFromNonAWTThread(final Project project) {
    //assert ApplicationManager.getApplication().isUnitTestMode();
    assert ! ApplicationManager.getApplication().isDispatchThread();
    myProject = project;
    myDone = false;
  }

  public void execute() {
    assert ! ApplicationManager.getApplication().isDispatchThread();
    final Object lock = new Object();

    synchronized (lock) {
      ChangeListManager.getInstance(myProject).invokeAfterUpdate(new Runnable() {
        public void run() {
          synchronized (lock) {
            myDone = true;
            lock.notifyAll();
          }
        }
      }, InvokeAfterUpdateMode.SILENT, null, null);

      while ((! myDone) && (! myProject.isDisposed())) {
        try {
          lock.wait();
        }
        catch (InterruptedException e) {
          //
        }
      }
    }
  }

  public boolean isDone() {
    return myDone;
  }
}

@@ -637,16 +637,61 @@
 anges;%0A%0A
+import com.intellij.diagnostic.ThreadDumper;%0A
 import c
@@ -786,16 +786,107 @@
 Project;
+%0Aimport com.intellij.util.concurrency.Semaphore;%0Aimport org.jetbrains.annotations.TestOnly;
 %0A%0A/**%0A *
@@ -974,16 +974,26 @@
 ate%0A */%0A
+@TestOnly%0A
 public c
@@ -1170,144 +1170,8 @@
 ) %7B%0A
-    //assert ApplicationManager.getApplication().isUnitTestMode();%0A    assert ! ApplicationManager.getApplication().isDispatchThread();%0A
     
@@ -1195,28 +1195,8 @@
 ct;%0A
-    myDone = false;%0A
   %7D%0A
@@ -1234,17 +1234,16 @@
 assert !
- 
 Applicat
@@ -1300,22 +1300,25 @@
   final 
-Object
+Semaphore
  lock = 
@@ -1325,47 +1325,40 @@
 new 
-Object();%0A%0A    synchronized (lock) %7B%0A  
+Semaphore();%0A%0A    lock.down();%0A%0A
     
@@ -1439,18 +1439,16 @@
 %7B%0A      
-  
 public v
@@ -1463,44 +1463,8 @@
 ) %7B%0A
-          synchronized (lock) %7B%0A    
     
@@ -1494,26 +1494,15 @@
     
-    
 lock.
-notifyAll
+up
 ();%0A
@@ -1511,26 +1511,10 @@
     
-    %7D%0A        %7D%0A  
+%7D%0A
     
@@ -1568,179 +1568,285 @@
     
-  
-while ((! myDone) && (! myProject.isDisposed())) %7B%0A        try %7B%0A          lock.wait();%0A        %7D%0A        catch (InterruptedException e) %7B%0A          //%0A        %7D%0A      %7D
+if (myProject.isDisposed()) return;%0A%0A    if (!lock.waitFor(100 * 1000) && !myProject.isDisposed()) %7B%0A      //noinspection UseOfSystemOutOrSystemErr%0A      System.out.println(ThreadDumper.dumpThreadsToString());%0A      throw new AssertionError(%22Couldn't wait for changes update%22);
 %0A   

add timeout to vcs tests waiting for changes view update