<commit_before>package brooklyn.entity.webapp;

import static brooklyn.test.TestUtils.*
import static java.util.concurrent.TimeUnit.*
import static org.testng.AssertJUnit.*

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.testng.annotations.AfterMethod
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test

import brooklyn.entity.Entity
import brooklyn.entity.basic.ApplicationBuilder
import brooklyn.entity.basic.Entities
import brooklyn.entity.proxying.EntitySpec
import brooklyn.entity.trait.Startable
import brooklyn.location.basic.SimulatedLocation
import brooklyn.test.EntityTestUtils
import brooklyn.test.entity.TestApplication
import brooklyn.test.entity.TestJavaWebAppEntity
import brooklyn.util.collections.MutableMap
import brooklyn.util.internal.TimeExtras

import com.google.common.collect.Iterables

/**
 * TODO clarify test purpose
 */
public class DynamicWebAppClusterTest {
    private static final Logger log = LoggerFactory.getLogger(this)
    
    private static final int TIMEOUT_MS = 1*1000;
    private static final int SHORT_WAIT_MS = 250;
    
    static { TimeExtras.init() }

    private TestApplication app;
    
    @BeforeMethod(alwaysRun=true)
    public void setUp() throws Exception {
        app = ApplicationBuilder.newManagedApp(TestApplication.class);
    }
    
    @AfterMethod(alwaysRun=true)
    public void tearDown() throws Exception {
        if (app != null) Entities.destroyAll(app.getManagementContext());
    }

    @Test
    public void testTestJWAEntity() {
        Entity test = app.createAndManageChild(EntitySpec.create(Entity.class, TestJavaWebAppEntity.class))
        try {
            test.invoke(Startable.START, [locations:[]]).get()
        } catch (Throwable t) {
            t.printStackTrace();
            throw t;
        }
//        ((Startable)test).start([])
    }
    
    @Test
    public void testRequestCountAggregation() {
        DynamicWebAppCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicWebAppCluster.class)
                .configure("initialSize", 2)
                .configure("factory", { properties -> new TestJavaWebAppEntity(properties) }));
        
        app.start([new SimulatedLocation()])
        
        cluster.members.each { it.spoofRequest() }
        executeUntilSucceeds(timeout: 3*SECONDS) {
            // intermittent failure observed 4 may 2012
            assertEquals 2, cluster.getAttribute(DynamicWebAppCluster.TOTAL_REQUEST_COUNT)
        }
        
        cluster.members.each { it.spoofRequest(); it.spoofRequest() }
        executeUntilSucceeds(timeout: 3*SECONDS) {
            assertEquals 3d, cluster.getAttribute(DynamicWebAppCluster.AVERAGE_REQUEST_COUNT)
        }
    }
    
    @Test
    public void testSetsServiceUpIfMemberIsUp() throws Exception {
        DynamicWebAppCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicWebAppCluster.class)
            .configure("initialSize", 1)
            .configure("factory", { properties -> new TestJavaWebAppEntity(properties) }));
    
        app.start([new SimulatedLocation()])
        
        // Should initially be false (when child has no service_up value) 
        EntityTestUtils.assertAttributeEqualsEventually(MutableMap.of("timeout", TIMEOUT_MS), cluster, DynamicWebAppCluster.SERVICE_UP, false);
        
        // When child is !service_up, should continue to report false
        Iterables.get(cluster.getMembers(), 0).setAttribute(Startable.SERVICE_UP, false);
        EntityTestUtils.assertAttributeEqualsContinually(MutableMap.of("timeout", SHORT_WAIT_MS), cluster, DynamicWebAppCluster.SERVICE_UP, false);
        
        cluster.resize(2);
        
        // When one of the two children is service_up, should report true
        Iterables.get(cluster.getMembers(), 0).setAttribute(Startable.SERVICE_UP, true);
        EntityTestUtils.assertAttributeEqualsEventually(MutableMap.of("timeout", TIMEOUT_MS), cluster, DynamicWebAppCluster.SERVICE_UP, true);

        // And if that serviceUp child goes away, should again report false
        Entities.unmanage(Iterables.get(cluster.getMembers(), 0));
        EntityTestUtils.assertAttributeEqualsEventually(MutableMap.of("timeout", TIMEOUT_MS), cluster, DynamicWebAppCluster.SERVICE_UP, false);<commit_msg>Fixed groovy tests after deleting deprecated code<commit_after>package brooklyn.entity.webapp;

import static brooklyn.test.TestUtils.*
import static java.util.concurrent.TimeUnit.*
import static org.testng.AssertJUnit.*

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.testng.annotations.AfterMethod
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test

import brooklyn.entity.Entity
import brooklyn.entity.basic.ApplicationBuilder
import brooklyn.entity.basic.Entities
import brooklyn.entity.proxying.EntitySpec
import brooklyn.entity.trait.Startable
import brooklyn.location.basic.SimulatedLocation
import brooklyn.test.EntityTestUtils
import brooklyn.test.entity.TestApplication
import brooklyn.test.entity.TestJavaWebAppEntity
import brooklyn.util.collections.MutableMap
import brooklyn.util.internal.TimeExtras

import com.google.common.collect.Iterables

/**
 * TODO clarify test purpose
 */
public class DynamicWebAppClusterTest {
    private static final Logger log = LoggerFactory.getLogger(this)
    
    private static final int TIMEOUT_MS = 1*1000;
    private static final int SHORT_WAIT_MS = 250;
    
    static { TimeExtras.init() }

    private TestApplication app;
    
    @BeforeMethod(alwaysRun=true)
    public void setUp() throws Exception {
        app = ApplicationBuilder.newManagedApp(TestApplication.class);
    }
    
    @AfterMethod(alwaysRun=true)
    public void tearDown() throws Exception {
        if (app != null) Entities.destroyAll(app.getManagementContext());
    }

    @Test
    public void testTestJWAEntity() {
        Entity test = app.createAndManageChild(EntitySpec.create(Entity.class, TestJavaWebAppEntity.class))
        try {
            test.invoke(Startable.START, [locations:[]]).get()
        } catch (Throwable t) {
            t.printStackTrace();
            throw t;
        }
//        ((Startable)test).start([])
    }
    
    @Test
    public void testRequestCountAggregation() {
        DynamicWebAppCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicWebAppCluster.class)
                .configure("initialSize", 2)
                .configure("factory", { properties -> new TestJavaWebAppEntity(properties) }));
        
        app.start([new SimulatedLocation()])
        
        cluster.members.each { it.spoofRequest() }
        executeUntilSucceeds(timeout: 3*SECONDS) {
            // intermittent failure observed 4 may 2012
            assertEquals 2, cluster.getAttribute(DynamicWebAppCluster.REQUEST_COUNT)
        }
        
        cluster.members.each { it.spoofRequest(); it.spoofRequest() }
        executeUntilSucceeds(timeout: 3*SECONDS) {
            assertEquals 3d, cluster.getAttribute(DynamicWebAppCluster.REQUEST_COUNT_PER_NODE)
        }
    }
    
    @Test
    public void testSetsServiceUpIfMemberIsUp() throws Exception {
        DynamicWebAppCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicWebAppCluster.class)
            .configure("initialSize", 1)
            .configure("factory", { properties -> new TestJavaWebAppEntity(properties) }));
    
        app.start([new SimulatedLocation()])
        
        // Should initially be false (when child has no service_up value) 
        EntityTestUtils.assertAttributeEqualsEventually(MutableMap.of("timeout", TIMEOUT_MS), cluster, DynamicWebAppCluster.SERVICE_UP, false);
        
        // When child is !service_up, should continue to report false
        Iterables.get(cluster.getMembers(), 0).setAttribute(Startable.SERVICE_UP, false);
        EntityTestUtils.assertAttributeEqualsContinually(MutableMap.of("timeout", SHORT_WAIT_MS), cluster, DynamicWebAppCluster.SERVICE_UP, false);
        
        cluster.resize(2);
        
        // When one of the two children is service_up, should report true
        Iterables.get(cluster.getMembers(), 0).setAttribute(Startable.SERVICE_UP, true);
        EntityTestUtils.assertAttributeEqualsEventually(MutableMap.of("timeout", TIMEOUT_MS), cluster, DynamicWebAppCluster.SERVICE_UP, true);

        // And if that serviceUp child goes away, should again report false
        Entities.unmanage(Iterables.get(cluster.getMembers(), 0));
        EntityTestUtils.assertAttributeEqualsEventually(MutableMap.of("timeout", TIMEOUT_MS), cluster, DynamicWebAppCluster.SERVICE_UP, false);
{'fraction_non_alphanumeric': 0.09268011527377522, 'fraction_numerical': 0.005763688760806916, 'mean_word_length': 3.493008803728638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '143773', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2352, 'n_words': 473}