<commit_before>/*
 *  Copyright (c) 2017, salesforce.com, inc.
 *  All rights reserved.
 *  Licensed under the BSD 3-Clause license.
 *  For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
 */

package com.salesforce.grpc.contrib.interceptor;

import com.salesforce.jprotoc.GreeterGrpc;
import com.salesforce.jprotoc.HelloRequest;
import com.salesforce.jprotoc.HelloResponse;
import io.grpc.ClientInterceptors;
import io.grpc.Context;
import io.grpc.Deadline;
import io.grpc.stub.StreamObserver;
import org.junit.Rule;
import io.grpc.testing.GrpcServerRule;
import org.junit.Test;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.*;

@SuppressWarnings("ALL")
public class DefaultDeadlineInterceptorTest {
    @Rule
    public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();

    @Test
    public void interceptorShouldAddDeadlineWhenAbsent() {
        TestGreeterService svc = new TestGreeterService();
        grpcServerRule.getServiceRegistry().addService(svc);

        GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(
                ClientInterceptors.intercept(grpcServerRule.getChannel(),
                        new DefaultDeadlineInterceptor(1, TimeUnit.SECONDS)));

        stub.sayHello(HelloRequest.getDefaultInstance());

        assertThat(svc.foundDeadline).isNotNull();
        assertThat(svc.foundDeadline.timeRemaining(TimeUnit.MILLISECONDS)).isGreaterThan(0);
        assertThat(svc.foundDeadline.timeRemaining(TimeUnit.MILLISECONDS)).isLessThan(1000);
    }

    @Test
    public void interceptorShouldNotModifyExplicitDeadline() {
        TestGreeterService svc = new TestGreeterService();
        grpcServerRule.getServiceRegistry().addService(svc);

        GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(
                ClientInterceptors.intercept(grpcServerRule.getChannel(),
                        new DefaultDeadlineInterceptor(1, TimeUnit.SECONDS)));

        stub.withDeadlineAfter(10, TimeUnit.SECONDS)
                .sayHello(HelloRequest.getDefaultInstance());

        assertThat(svc.foundDeadline).isNotNull();
        assertThat(svc.foundDeadline.timeRemaining(TimeUnit.MILLISECONDS)).isGreaterThan(9000);
    }

    @Test
    public void interceptorShouldNotModifyContextDeadline() throws Exception {
        TestGreeterService svc = new TestGreeterService();
        grpcServerRule.getServiceRegistry().addService(svc);

        Context.current()
                .withDeadlineAfter(10, TimeUnit.SECONDS, Executors.newSingleThreadScheduledExecutor())
                .run(() -> {
                    GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(
                            ClientInterceptors.intercept(grpcServerRule.getChannel(),
                                    new DefaultDeadlineInterceptor(1, TimeUnit.SECONDS)));
                    stub.sayHello(HelloRequest.getDefaultInstance());
                });

        assertThat(svc.foundDeadline).isNotNull();
        assertThat(svc.foundDeadline.timeRemaining(TimeUnit.MILLISECONDS)).isGreaterThan(9000);
    }

    private static class TestGreeterService extends GreeterGrpc.GreeterImplBase {
        private Deadline foundDeadline;

        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
            foundDeadline = Context.current().getDeadline();
            responseObserver.onNext(HelloResponse.getDefaultInstance());
            responseObserver.onCompleted();
        }
    }
}<commit_msg>Rewrite as unit test instead of integration test<commit_after>/*
 *  Copyright (c) 2017, salesforce.com, inc.
 *  All rights reserved.
 *  Licensed under the BSD 3-Clause license.
 *  For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
 */

package com.salesforce.grpc.contrib.interceptor;

import io.grpc.*;
import org.junit.Test;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("ALL")
public class DefaultDeadlineInterceptorTest {
    @Test
    public void interceptorShouldAddDeadlineWhenAbsent() {
        AtomicBoolean called = new AtomicBoolean(false);

        DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);

        interceptor.interceptCall(null, CallOptions.DEFAULT, new Channel() {
            @Override
            public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
                called.set(true);
                assertThat(callOptions.getDeadline().timeRemaining(TimeUnit.MINUTES)).isEqualTo(59);
                return null;
            }

            @Override
            public String authority() {
                return null;
            }
        });

        assertThat(called.get()).isTrue();
    }

    @Test
    public void interceptorShouldNotModifyExplicitDeadline() {
        AtomicBoolean called = new AtomicBoolean(false);

        DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);

        interceptor.interceptCall(null, CallOptions.DEFAULT.withDeadlineAfter(10, TimeUnit.HOURS), new Channel() {
            @Override
            public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
                called.set(true);
                assertThat(callOptions.getDeadline().timeRemaining(TimeUnit.HOURS)).isEqualTo(9);
                return null;
            }

            @Override
            public String authority() {
                return null;
            }
        });

        assertThat(called.get()).isTrue();
    }

    @Test
    public void interceptorShouldNotModifyContextDeadline() throws Exception {
        AtomicBoolean called = new AtomicBoolean(false);

        DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);

        Context.current().withDeadlineAfter(10, TimeUnit.HOURS, Executors.newSingleThreadScheduledExecutor()).run(() -> {
            interceptor.interceptCall(null, CallOptions.DEFAULT, new Channel() {
                @Override
                public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
                    called.set(true);
                    assertThat(callOptions.getDeadline()).isNull();
                    return null;
                }

                @Override
                public String authority() {
                    return null;
                }
            });
        });

        assertThat(called.get()).isTrue();
    }
}
{'fraction_non_alphanumeric': 0.0930628457937296, 'fraction_numerical': 0.005958291956305859, 'mean_word_length': 2.9407490217998884, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2376515', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1877, 'n_words': 332}