<commit_before>// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.jdisc;

import com.google.inject.AbstractModule;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.application.BindingSetSelector;
import com.yahoo.jdisc.handler.*;
import com.yahoo.messagebus.Error;
import com.yahoo.messagebus.*;
import com.yahoo.messagebus.jdisc.test.ServerTestDriver;
import com.yahoo.messagebus.shared.ServerSession;
import com.yahoo.messagebus.test.SimpleMessage;
import com.yahoo.messagebus.test.SimpleReply;
import org.junit.Test;

import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;

/**
 * @author <NAME>
 */
public class MbusServerTestCase {

    @Test
    public void requireThatServerRetainsSession() {
        MySession session = new MySession();
        assertEquals(1, session.refCount);
        MbusServer server = new MbusServer(null, session);
        assertEquals(2, session.refCount);
        session.release();
        assertEquals(1, session.refCount);
        server.destroy();
        assertEquals(0, session.refCount);
    }

    @Test
    public void requireThatNoBindingSetSelectedExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newUnboundInstance(true, new MySelector(null));
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatBindingSetNotFoundExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newUnboundInstance(true, new MySelector("foo"));
        assertTrue(driver.sendMessage(new SimpleMessage("bar")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatContainerNotReadyExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newInactiveInstance(true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatBindingNotFoundExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newUnboundInstance(true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatRequestDeniedExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newInstance(MyRequestHandler.newRequestDenied(), true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatRequestResponseWorks() {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        assertNotNull(requestHandler.awaitRequest());
        assertTrue(requestHandler.sendResponse(new Response(Response.Status.OK)));

        assertNotNull(driver.awaitSuccess());
        assertTrue(driver.close());
    }

    @Test
    public void requireThatRequestIsMbus() {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        Request request = requestHandler.awaitRequest();
        assertTrue(request instanceof MbusRequest);
        Message msg = ((MbusRequest)request).getMessage();
        assertTrue(msg instanceof SimpleMessage);
        assertEquals("foo", ((SimpleMessage)msg).getValue());
        assertTrue(requestHandler.sendResponse(new Response(Response.Status.OK)));

        assertNotNull(driver.awaitSuccess());
        assertTrue(driver.close());
    }

    @Test
    public void requireThatReplyInsideMbusResponseIsUsed() {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        assertNotNull(requestHandler.awaitRequest());
        Reply reply = new SimpleReply("bar");
        reply.swapState(((MbusRequest)requestHandler.request).getMessage());
        assertTrue(requestHandler.sendResponse(new MbusResponse(Response.Status.OK, reply)));

        reply = driver.awaitSuccess();
        assertTrue(reply instanceof SimpleReply);
        assertEquals("bar", ((SimpleReply)reply).getValue());

        assertTrue(driver.close());
    }

    @Test
    public void requireThatNonMbusResponseCausesEmptyReply() {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, false);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        assertNotNull(requestHandler.awaitRequest());
        assertTrue(requestHandler.sendResponse(new Response(Response.Status.OK)));

        assertNotNull(driver.awaitSuccess());
        assertTrue(driver.close());
    }

    @Test
    public void requireThatMbusRequestContentCallsCompletion() throws InterruptedException {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        assertNotNull(requestHandler.awaitRequest());
        ContentChannel content = requestHandler.responseHandler.handleResponse(new Response(Response.Status.OK));
        assertNotNull(content);
        MyCompletion completion = new MyCompletion();
        content.close(completion);<commit_msg>Don't setup network in tests that don't need it<commit_after>// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.jdisc;

import com.google.inject.AbstractModule;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.application.BindingSetSelector;
import com.yahoo.jdisc.handler.*;
import com.yahoo.messagebus.Error;
import com.yahoo.messagebus.*;
import com.yahoo.messagebus.jdisc.test.ServerTestDriver;
import com.yahoo.messagebus.shared.ServerSession;
import com.yahoo.messagebus.test.SimpleMessage;
import com.yahoo.messagebus.test.SimpleReply;
import org.junit.Test;

import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;

/**
 * @author <NAME>
 */
public class MbusServerTestCase {

    @Test
    public void requireThatServerRetainsSession() {
        MySession session = new MySession();
        assertEquals(1, session.refCount);
        MbusServer server = new MbusServer(null, session);
        assertEquals(2, session.refCount);
        session.release();
        assertEquals(1, session.refCount);
        server.destroy();
        assertEquals(0, session.refCount);
    }

    @Test
    public void requireThatNoBindingSetSelectedExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newUnboundInstance(true, new MySelector(null));
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatBindingSetNotFoundExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newUnboundInstance(true, new MySelector("foo"));
        assertTrue(driver.sendMessage(new SimpleMessage("bar")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatContainerNotReadyExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newInactiveInstance(true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatBindingNotFoundExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newUnboundInstance(true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatRequestDeniedExceptionIsCaught() {
        ServerTestDriver driver = ServerTestDriver.newInstance(MyRequestHandler.newRequestDenied(), true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));
        assertNotNull(driver.awaitErrors(ErrorCode.APP_FATAL_ERROR));
        assertTrue(driver.close());
    }

    @Test
    public void requireThatRequestResponseWorks() {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        assertNotNull(requestHandler.awaitRequest());
        assertTrue(requestHandler.sendResponse(new Response(Response.Status.OK)));

        assertNotNull(driver.awaitSuccess());
        assertTrue(driver.close());
    }

    @Test
    public void requireThatRequestIsMbus() {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        Request request = requestHandler.awaitRequest();
        assertTrue(request instanceof MbusRequest);
        Message msg = ((MbusRequest)request).getMessage();
        assertTrue(msg instanceof SimpleMessage);
        assertEquals("foo", ((SimpleMessage)msg).getValue());
        assertTrue(requestHandler.sendResponse(new Response(Response.Status.OK)));

        assertNotNull(driver.awaitSuccess());
        assertTrue(driver.close());
    }

    @Test
    public void requireThatReplyInsideMbusResponseIsUsed() {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        assertNotNull(requestHandler.awaitRequest());
        Reply reply = new SimpleReply("bar");
        reply.swapState(((MbusRequest)requestHandler.request).getMessage());
        assertTrue(requestHandler.sendResponse(new MbusResponse(Response.Status.OK, reply)));

        reply = driver.awaitSuccess();
        assertTrue(reply instanceof SimpleReply);
        assertEquals("bar", ((SimpleReply)reply).getValue());

        assertTrue(driver.close());
    }

    @Test
    public void requireThatNonMbusResponseCausesEmptyReply() {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        assertNotNull(requestHandler.awaitRequest());
        assertTrue(requestHandler.sendResponse(new Response(Response.Status.OK)));

        assertNotNull(driver.awaitSuccess());
        assertTrue(driver.close());
    }

    @Test
    public void requireThatMbusRequestContentCallsCompletion() throws InterruptedException {
        MyRequestHandler requestHandler = MyRequestHandler.newInstance();
        ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler, true);
        assertTrue(driver.sendMessage(new SimpleMessage("foo")));

        assertNotNull(requestHandler.awaitRequest());
        ContentChannel content = requestHandler.responseHandler.handleResponse(new Response(Response.Status.OK));
        assertNotNull(content);
        MyCompletion completion = new MyCompletion();
        content.close(completion);
{'fraction_non_alphanumeric': 0.1043026822937151, 'fraction_numerical': 0.001573192794777, 'mean_word_length': 4.208521097910692, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6082572', 'n_tokens_mistral': 3213, 'n_tokens_neox': 3224, 'n_words': 563}