Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7be24ba0 authored by Kihong Seong's avatar Kihong Seong
Browse files

Add helper method for setting up initial environment for Bumble BT tests

Adds helper method for setting up Bumble BT tests. The function terminates unusable previous channels and help create a new channel

Bug: 296790072
Test: atest BumbleBluetoothTests
Change-Id: Ib9b56ee39dbf3e0c88432603f260a5d7e63d7ea2
parent 9881e645
Loading
Loading
Loading
Loading
+4 −20
Original line number Diff line number Diff line
package android.bluetooth;

import static android.bluetooth.Utils.factoryResetAndCreateNewChannel;

import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertisingSet;
import android.bluetooth.le.AdvertisingSetCallback;
@@ -18,7 +20,6 @@ import com.google.protobuf.Empty;
import io.grpc.Context.CancellableContext;
import io.grpc.Deadline;
import io.grpc.ManagedChannel;
import io.grpc.okhttp.OkHttpChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.util.concurrent.CompletableFuture;
@@ -59,25 +60,8 @@ public class LeAdvertisingTest {

    @Before
    public void setUp() throws Exception {
        // FactorReset is killing the server and restart
        // all channel created before the server restarted
        // cannot be reused
        ManagedChannel channel = OkHttpChannelBuilder
              .forAddress("localhost", 7999)
              .usePlaintext()
              .build();

        HostGrpc.HostBlockingStub stub = HostGrpc.newBlockingStub(channel);
        stub.factoryReset(Empty.getDefaultInstance());

        // terminate the channel
        channel.shutdown().awaitTermination(1, TimeUnit.SECONDS);

        // Create a new channel for all successive grpc calls
        mChannel = OkHttpChannelBuilder
              .forAddress("localhost", 7999)
              .usePlaintext()
              .build();
        // Cleanup previous channels and create a new channel for all successive grpc calls
        mChannel = factoryResetAndCreateNewChannel();

        mHostBlockingStub = HostGrpc.newBlockingStub(mChannel);
        mHostStub = HostGrpc.newStub(mChannel);
+5 −16
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.bluetooth;

import static android.bluetooth.Utils.factoryResetAndCreateNewChannel;

import static com.google.common.truth.Truth.assertThat;

import android.bluetooth.le.BluetoothLeScanner;
@@ -45,7 +47,6 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;
import io.grpc.okhttp.OkHttpChannelBuilder;
import io.grpc.stub.StreamObserver;

import pandora.HostGrpc;
@@ -56,7 +57,7 @@ import pandora.HostProto.AdvertiseResponse;
@RunWith(AndroidJUnit4.class)
public class LeScanningTest {
    private static final String TAG = "LeScanningTest";
    private static final int TIMEOUT_SCANNING_MS = 1000;
    private static final int TIMEOUT_SCANNING_MS = 2000;

    private static ManagedChannel mChannel;

@@ -75,20 +76,8 @@ public class LeScanningTest {

    @Before
    public void setUp() throws Exception {
        // FactorReset is killing the server and restart
        // all channel created before the server restarted
        // cannot be reused
        ManagedChannel channel =
                OkHttpChannelBuilder.forAddress("localhost", 7999).usePlaintext().build();

        HostGrpc.HostBlockingStub stub = HostGrpc.newBlockingStub(channel);
        stub.factoryReset(Empty.getDefaultInstance());

        // terminate the channel
        channel.shutdown().awaitTermination(1, TimeUnit.SECONDS);

        // Create a new channel for all successive grpc calls
        mChannel = OkHttpChannelBuilder.forAddress("localhost", 7999).usePlaintext().build();
        // Cleanup previous channels and create a new channel for all successive grpc calls
        mChannel = factoryResetAndCreateNewChannel();

        mHostBlockingStub = HostGrpc.newBlockingStub(mChannel);
        mHostStub = HostGrpc.newStub(mChannel);
+25 −0
Original line number Diff line number Diff line
package android.bluetooth;

import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;
import io.grpc.okhttp.OkHttpChannelBuilder;
import pandora.HostGrpc;

public final class Utils {
    public static String addressStringFromByteString(ByteString bs) {
@@ -13,4 +20,22 @@ public final class Utils {
        }
        return refAddrBuilder.toString();
    }

    public static ManagedChannel factoryResetAndCreateNewChannel() throws InterruptedException {
        // FactoryReset is killing the server and restarting all channels created before the server
        // restarted that cannot be reused
        ManagedChannel channel = OkHttpChannelBuilder
                .forAddress("localhost", 7999)
                .usePlaintext()
                .build();

        HostGrpc.HostBlockingStub stub = HostGrpc.newBlockingStub(channel);
        stub.factoryReset(Empty.getDefaultInstance());

        // terminate the channel
        channel.shutdown().awaitTermination(1, TimeUnit.SECONDS);

        // return new channel for future use
        return OkHttpChannelBuilder.forAddress("localhost", 7999).usePlaintext().build();
    }
}