我需要在destroyTask()
中测试对setInterval()
的调用setInterval()
比较两个时间戳,当它们的当前时间等于destroyTask()
时,它们调用finishTime
async jobTimeout() {
if (!this.timeout || !this.timeunits) return;
const startTime = moment();
const finishTime = moment().add(this.timeout, this.timeunits);
// console.log('Duration - ', this.timeout, ' ' ,this.timeunits);
console.log(`Start Time: ${startTime}`);
console.log(`Finish Time: ${finishTime}`);
this._timer = setInterval(() => {
console.log(moment());
if (moment().isAfter(finishTime)) {
console.log("The task didn't finish in time. ", this.destroyTask());
clearInterval(this._timer); // the clearInterval() method clears a timer set with the setInterval() method
}
}, 1000);
}
setInterval
我正在使用sinon监视两个函数,并使用伪造的计时器覆盖 describe('Generic Setup', () => {
beforeEach(() => {
// Overwrite the global timer functions (setTimeout, setInterval) with Sinon fakes
this.clock = sinon.useFakeTimers();
});
afterEach(() => {
// Restore the global timer functions to their native implementations
this.clock.restore();
});
it('Job Should Timeout After Specified Duration', async () => {
const task = new TASK(taskData, done);
const result = sinon.spy(await task.jobTimeout());
const destroyTask = sinon.spy(await task.destroyTask());
setInterval(result, 1000);
this.clock.tick(4000);
expect(destroyTask).to.not.have.been.called;
this.clock.tick(1000);
expect(destroyTask).to.have.been.called;
});
});
。我的测试用例如下:
TypeError: this.done is not a function
我收到以下错误destroyTask
。我认为是async destroyTask() {
// TODO cleanup tmp directory - might want to collect stat on error
clearInterval(this._timer);
this.done();
}
引起的,但这是一个函数。
package com.example.obd2_bluetooth_communication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelUuid;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.UUID;
import br.ufrn.imd.obd.commands.ObdCommandGroup;
import br.ufrn.imd.obd.commands.engine.RPMCommand;
import br.ufrn.imd.obd.commands.protocol.EchoOffCommand;
import br.ufrn.imd.obd.commands.protocol.LineFeedOffCommand;
import br.ufrn.imd.obd.commands.protocol.SelectProtocolCommand;
import br.ufrn.imd.obd.commands.protocol.TimeoutCommand;
import br.ufrn.imd.obd.enums.ObdProtocols;
import static android.graphics.Color.RED;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
TextView tvEstadoBT, tvRPM;
ImageView ivEstadoBT;
Button btnOnOff, btnPaired, btnEnviar;
BluetoothAdapter mBlueAdapter;
Set<BluetoothDevice> paired_devices;
String plist[];
ListView lvPairedDevices;
ArrayAdapter<String> mAdapter;
ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();
private static UUID myuuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothSocket socket;
//UUID theuuid;
RPMCommand rpmCommand = new RPMCommand();
static final int STATE_CONFIG = 1;
static final int STATE_CONNECTED = 2;
static final int STATE_CONNECTION_FAILED = 3;
static final int STATE_ERROR = 4;
static final int STATE_INTERRUPT = 5;
static final int STATE_DATO_ENVIADO = 6;
private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (action.equals(mBlueAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBlueAdapter.ERROR);
switch (state){
case BluetoothAdapter.STATE_OFF:
showToast("Bluetooth apagado");
btnOnOff.setText("Encender Bluetooth");
tvEstadoBT.setText("Bluetooth apagado");
tvEstadoBT.setTextColor(RED);
ivEstadoBT.setImageResource(R.drawable.ic_action_off);
btnEnviar.setEnabled(false);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
showToast("Apagando...");
break;
case BluetoothAdapter.STATE_ON:
showToast("Bluetooth encendido");
btnOnOff.setText("Apagar Bluetooth");
tvEstadoBT.setText("Bluetooth encendido");
tvEstadoBT.setTextColor(Color.GREEN);
ivEstadoBT.setImageResource(R.drawable.ic_action_on);
break;
case BluetoothAdapter.STATE_TURNING_ON:
showToast("Encendiendo...");
break;
}
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBroadcastReceiver1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvEstadoBT = findViewById(R.id.tvEstadoBT);
ivEstadoBT = findViewById(R.id.ivEstadoBT);
btnOnOff = findViewById(R.id.btnOnOff);
btnPaired = findViewById(R.id.btnPaired);
lvPairedDevices = findViewById(R.id.lvPairedDevices);
btnEnviar = findViewById(R.id.btnSend);
tvRPM = findViewById(R.id.tvRPM);
mBlueAdapter = BluetoothAdapter.getDefaultAdapter();
//Verificar si el dispositivo posee Bluetooth
if (mBlueAdapter == null) {
showToast("Tu dispositivo no soporta la opción de Bluetooth");
}
//Condiciones iniciales para el botón de encendido/apagado
if (!mBlueAdapter.isEnabled()) {
btnOnOff.setText("Encender Bluetooth");
tvEstadoBT.setText("Bluetooth apagado");
tvEstadoBT.setTextColor(RED);
ivEstadoBT.setImageResource(R.drawable.ic_action_off);
}
if (mBlueAdapter.isEnabled()) {
btnOnOff.setText("Apagar Bluetooth");
tvEstadoBT.setText("Bluetooth encendido");
tvEstadoBT.setTextColor(Color.GREEN);
ivEstadoBT.setImageResource(R.drawable.ic_action_on);
}
}
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
switch(msg.what)
{
case STATE_CONFIG:
showToast("Configurado");
btnEnviar.setEnabled(true);
finish();
break;
case STATE_CONNECTED:
showToast("Conectado");
btnEnviar.setEnabled(true);
finish();
break;
case STATE_CONNECTION_FAILED:
showToast("Conexión falló");
finish();
break;
case STATE_ERROR:
showToast("Ocurrió un error");
;
break;
case STATE_INTERRUPT:
showToast("Función interrumpida");
break;
case STATE_DATO_ENVIADO:
tvRPM.setText(rpmCommand.getFormattedResult() + " rpm");
break;
}
return false;
}
});
private void showToast(String msg){
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
public void OnOffCLICK(View view) {
if(!mBlueAdapter.isEnabled()){
//showToast("Activando Bluetooth...");
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableIntent);
IntentFilter BTIntent = new IntentFilter((BluetoothAdapter.ACTION_STATE_CHANGED));
registerReceiver(mBroadcastReceiver1, BTIntent);
}
if(mBlueAdapter.isEnabled()){
mBlueAdapter.disable();
IntentFilter BTIntent = new IntentFilter((BluetoothAdapter.ACTION_STATE_CHANGED));
registerReceiver(mBroadcastReceiver1, BTIntent);
}
}
public void PairedDevicesCLICK(View view) {
if(!mBlueAdapter.isEnabled()){
showToast("La función no está disponible porque el Bluettoth está apagado");
}
else{
paired_devices = mBlueAdapter.getBondedDevices();
mBTDevices = new ArrayList<>();
int count = paired_devices.size();
plist = new String[count];
int j = 0;
for(BluetoothDevice device : paired_devices)
{
mBTDevices.add(device);
plist[j] = device.getName();
j++;
}
mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,plist);
lvPairedDevices.setAdapter(mAdapter);
lvPairedDevices.setOnItemClickListener(MainActivity.this);
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
/*String deviceName = mBTDevices.get(i).getName();
String deviceAddress = mBTDevices.get(i).getAddress();
showToast("Connecting to: " + deviceName);*/
/*ParcelUuid[] uuids = mBTDevices.get(i).getUuids();
theuuid = UUID.fromString(uuids[0].toString());
showToast(theuuid.toString());*/
ClientClass cliente = new ClientClass(mBTDevices.get(i));
cliente.start();
}
public void EnviarCLICK(View view) {
ObdCommandGroup obdCommands = new ObdCommandGroup();
obdCommands.add(new EchoOffCommand());
obdCommands.add(new LineFeedOffCommand());
obdCommands.add(new TimeoutCommand(255));
obdCommands.add(new SelectProtocolCommand(ObdProtocols.AUTO));
//obdCommands.add(new RPMCommand());
obdCommands.add(rpmCommand);
try {
obdCommands.run(socket.getInputStream(), socket.getOutputStream());
Message message = Message.obtain();
message.what = STATE_DATO_ENVIADO;
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
Message message = Message.obtain();
message.what = STATE_ERROR;
handler.sendMessage(message);
} catch (InterruptedException e) {
e.printStackTrace();
Message message = Message.obtain();
message.what = STATE_INTERRUPT;
handler.sendMessage(message);
}
}
private class ClientClass extends Thread{
private BluetoothDevice OBD2;
private BluetoothSocket socket;
public ClientClass (BluetoothDevice device1)
{
OBD2 = device1;
/*ParcelUuid[] uuids = OBD2.getUuids();
if (uuids != null)
{
theuuid = UUID.fromString(uuids[0].toString());
//showToast(theuuid.toString());
}*/
try {
//socket = OBD2.createInsecureRfcommSocketToServiceRecord(theuuid);
socket = OBD2.createInsecureRfcommSocketToServiceRecord(myuuid);
} catch (IOException e) {
e.printStackTrace();
Message message = Message.obtain();
message.what = STATE_ERROR;
handler.sendMessage(message);
}
}
public void run()
{
mBlueAdapter.cancelDiscovery();
try {
socket.connect();
Message message = Message.obtain();
message.what = STATE_CONNECTED;
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
try {
socket.close();
Message message = Message.obtain();
message.what = STATE_CONNECTION_FAILED;
handler.sendMessage(message);
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}
}
}
}
可能是什么原因?是否可以在另一个函数中测试对外部函数的回调?
答案 0 :(得分:0)
不得不修改一些东西。我已经更改了间谍电话,将第二个this.clock.tick()
增加到2000
,并且必须在类实例化中提供自定义的done
函数。
describe('Generic Setup', () => {
beforeEach(() => {
// Overwrite the global timer functions (setTimeout, setInterval) with Sinon fakes
this.clock = sinon.useFakeTimers();
});
afterEach(() => {
// Restore the global timer functions to their native implementations
this.clock.restore();
});
it('Job Should Timeout After Specified Duration', async () => {
const task = new TASK(taskData, () => {});
task.jobTimeout();
const destroyTaskSpy = sinon.spy(task, 'destroyTask');
this.clock.tick(4000);
expect(destroyTaskSpy).to.not.have.been.called;
this.clock.tick(2000);
expect(destroyTaskSpy).to.have.been.called;
// Remove the spy to prevent future errors
destroyTaskSpy.restore();
});
});