/*
 * Copyright 2010 Ning, Inc.
 *
 * Ning licenses this file to you 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.ning.http.client.async;

import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.Response;
import org.apache.log4j.BasicConfigurator;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;


public class RemoteSiteTest {

    private AsyncHttpClient c;
    private CyclicBarrier b;
    private AsyncHandler h;
    private Throwable t;

    @BeforeClass
    public void before() {
        BasicConfigurator.configure();
        b = new CyclicBarrier(2);
        c = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeout(10000).build());
        t = null;
        h = new AsyncHandler() {
            public void onThrowable(Throwable t) {
                try {
                    RemoteSiteTest.this.t = t;
                }
                finally {
                    try {
                        b.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }

            public Object onCompleted(Response response) throws IOException {
                try {
                    // never reached :(
                    return null;
                } finally {
                    try {
                        b.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }

    @AfterClass
    public void checkThrowable() {
        if (t != null) {
            t.printStackTrace();
            Assert.fail("timeout?!");
        }
    }

    @Test
    public void testGoogleCom() throws IOException, BrokenBarrierException, InterruptedException {
        // Works
        c.prepareGet("http://google.com/").execute(h);
        b.await();
    }

    @Test
    public void testMailGoogleCom() throws IOException, BrokenBarrierException, InterruptedException {
        c.prepareGet("http://mail.google.com/").execute(h);
        b.await();
    }

    @Test
    public void testPlanetromeoCom() throws IOException, BrokenBarrierException, InterruptedException {
        c.prepareGet("http://planetromeo.com/").execute(h);
        b.await();
    }

    @Test
    public void testMiniPlanetromeoCom() throws IOException, BrokenBarrierException, InterruptedException {
        c.prepareGet("http://mini.planetromeo.com/").execute(h);
        b.await();
    }

    @Test
    public void testMicrosoftCom() throws IOException, BrokenBarrierException, InterruptedException {
        // Works
        c.prepareGet("http://microsoft.com/").execute(h);
        b.await();
    }

    @Test
    public void testWwwMicrosoftCom() throws IOException, BrokenBarrierException, InterruptedException {
        c.prepareGet("http://www.microsoft.com/").execute(h);
        b.await();
    }

    @Test
    public void testUpdateMicrosoftCom() throws IOException, BrokenBarrierException, InterruptedException {
        c.prepareGet("http://update.microsoft.com/").execute(h);
        b.await();
    }
}


@@ -1140,16 +1140,153 @@
 rier;%0A%0A%0A
+/**%0A * Unit tests for remote site.%0A *%0A * see http://github.com/MSch/ning-async-http-client-bug/tree/master%0A * @author Martin Schrrer%0A */%0A
 public c

Add Martin as the author of this class