Fix testWifiStateChangedListener failure caused by race condition
testWifiStateChangedListener relies on WifiStateChangedListener to be
called after registering and calling WifiManager#setWifiEnabled(false).
However, a race with a previous call to #setWifiEnabled may cause
ActiveModeWarden to trigger the state changed callbacks/listeners before
registering the callback on the Wifi thread.
e.g.
Binder: setWifiEnabled(true)
Binder: mPersistWifiState = WIFI_ENABLED
Binder: CMD_WIFI_TOGGLED(1) posted to Wifi thread
Binder: addWifiStateChangedListener posted to Wifi thread
Binder: setWifiEnabled(false)
Binder: mPersistWifiState = WIFI_DISABLED
Binder: CMD_WIFI_TOGGLED(2) posted to Wifi thread
Wifi: Handle CMD_WIFI_TOGGLED(1)
Wifi: mPersistWifiSate == WIFI_DISABLED, disable Wifi and call listeners
Wifi: Run addWifiStateChangedListener, but we missed the
ENABLED->DISABLED callbacks even though we called setWifiEnabled(false)
after registering.
Wifi: Handle CMD_WIFI_TOGGLED(2)
Wifi: mPersistWifiState == WIFI_DISABLED, do nothing
Fix this by making sure we wait for the full state change before
proceeding with the call to disable. Since WifiManagerTest#setUp already
enables Wifi and waits for the state change, we can remove the redundant
call to setWifiEnabled(true) so that there's no pending CMD_WIFI_TOGGLED
that may race with the listener registration.
Flag: EXEMPT minor test flake fix
Bug: 391553325
Test: atest
android.net.wifi.cts.WifiManagerTest#testWifiStateChangedListener
Change-Id: Icdf08f55085f3cd223718055638f7ec0bebf227d
1 file changed