1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Unit Test for TranscodingSessionController
18 
19 // #define LOG_NDEBUG 0
20 #define LOG_TAG "TranscodingSessionControllerTest"
21 
22 #include <aidl/android/media/BnTranscodingClientCallback.h>
23 #include <aidl/android/media/IMediaTranscodingService.h>
24 #include <aidl/android/media/ITranscodingClient.h>
25 #include <aidl/android/media/ITranscodingClientCallback.h>
26 #include <android-base/logging.h>
27 #include <android/binder_manager.h>
28 #include <android/binder_process.h>
29 #include <gtest/gtest.h>
30 #include <media/TranscodingClientManager.h>
31 #include <media/TranscodingSessionController.h>
32 #include <utils/Log.h>
33 
34 #include <unordered_set>
35 
36 namespace android {
37 
38 using Status = ::ndk::ScopedAStatus;
39 using aidl::android::media::BnTranscodingClientCallback;
40 using aidl::android::media::IMediaTranscodingService;
41 using aidl::android::media::ITranscodingClient;
42 using aidl::android::media::TranscodingRequestParcel;
43 
44 constexpr ClientIdType kClientId = 1000;
45 constexpr SessionIdType kClientSessionId = 0;
46 constexpr uid_t kClientUid = 5000;
47 constexpr pid_t kClientPid = 10000;
48 constexpr uid_t kInvalidUid = (uid_t)-1;
49 constexpr pid_t kInvalidPid = (pid_t)-1;
50 
51 #define CLIENT(n) (kClientId + (n))
52 #define SESSION(n) (kClientSessionId + (n))
53 #define UID(n) (kClientUid + (n))
54 #define PID(n) (kClientPid + (n))
55 
56 class TestUidPolicy : public UidPolicyInterface {
57 public:
58     TestUidPolicy() = default;
59     virtual ~TestUidPolicy() = default;
60 
61     // UidPolicyInterface
registerMonitorUid(uid_t)62     void registerMonitorUid(uid_t /*uid*/) override {}
unregisterMonitorUid(uid_t)63     void unregisterMonitorUid(uid_t /*uid*/) override {}
isUidOnTop(uid_t uid)64     bool isUidOnTop(uid_t uid) override { return mTopUids.count(uid) > 0; }
getTopUids() const65     std::unordered_set<uid_t> getTopUids() const override { return mTopUids; }
setCallback(const std::shared_ptr<UidPolicyCallbackInterface> & cb)66     void setCallback(const std::shared_ptr<UidPolicyCallbackInterface>& cb) override {
67         mUidPolicyCallback = cb;
68     }
setTop(uid_t uid)69     void setTop(uid_t uid) {
70         std::unordered_set<uid_t> uids = {uid};
71         setTop(uids);
72     }
setTop(const std::unordered_set<uid_t> & uids)73     void setTop(const std::unordered_set<uid_t>& uids) {
74         mTopUids = uids;
75         auto uidPolicyCb = mUidPolicyCallback.lock();
76         if (uidPolicyCb != nullptr) {
77             uidPolicyCb->onTopUidsChanged(mTopUids);
78         }
79     }
80 
81     std::unordered_set<uid_t> mTopUids;
82     std::weak_ptr<UidPolicyCallbackInterface> mUidPolicyCallback;
83 };
84 
85 class TestResourcePolicy : public ResourcePolicyInterface {
86 public:
TestResourcePolicy()87     TestResourcePolicy() { reset(); }
88     virtual ~TestResourcePolicy() = default;
89 
90     // ResourcePolicyInterface
setCallback(const std::shared_ptr<ResourcePolicyCallbackInterface> &)91     void setCallback(const std::shared_ptr<ResourcePolicyCallbackInterface>& /*cb*/) override {}
setPidResourceLost(pid_t pid)92     void setPidResourceLost(pid_t pid) override { mResourceLostPid = pid; }
93     // ~ResourcePolicyInterface
94 
getPid()95     pid_t getPid() {
96         pid_t result = mResourceLostPid;
97         reset();
98         return result;
99     }
100 
101 private:
reset()102     void reset() { mResourceLostPid = kInvalidPid; }
103     pid_t mResourceLostPid;
104 };
105 
106 class TestThermalPolicy : public ThermalPolicyInterface {
107 public:
108     TestThermalPolicy() = default;
109     virtual ~TestThermalPolicy() = default;
110 
111     // ThermalPolicyInterface
setCallback(const std::shared_ptr<ThermalPolicyCallbackInterface> &)112     void setCallback(const std::shared_ptr<ThermalPolicyCallbackInterface>& /*cb*/) override {}
getThrottlingStatus()113     bool getThrottlingStatus() { return false; }
114     // ~ThermalPolicyInterface
115 
116 private:
117 };
118 
119 class TestTranscoder : public TranscoderInterface {
120 public:
TestTranscoder()121     TestTranscoder() : mGeneration(0) {}
~TestTranscoder()122     virtual ~TestTranscoder() {}
123 
124     // TranscoderInterface
start(ClientIdType clientId,SessionIdType sessionId,const TranscodingRequestParcel &,uid_t,const std::shared_ptr<ITranscodingClientCallback> &)125     void start(ClientIdType clientId, SessionIdType sessionId,
126                const TranscodingRequestParcel& /*request*/, uid_t /*callingUid*/,
127                const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) override {
128         append(Start(clientId, sessionId));
129     }
pause(ClientIdType clientId,SessionIdType sessionId)130     void pause(ClientIdType clientId, SessionIdType sessionId) override {
131         append(Pause(clientId, sessionId));
132     }
resume(ClientIdType clientId,SessionIdType sessionId,const TranscodingRequestParcel &,uid_t,const std::shared_ptr<ITranscodingClientCallback> &)133     void resume(ClientIdType clientId, SessionIdType sessionId,
134                 const TranscodingRequestParcel& /*request*/, uid_t /*callingUid*/,
135                 const std::shared_ptr<ITranscodingClientCallback>& /*clientCallback*/) override {
136         append(Resume(clientId, sessionId));
137     }
stop(ClientIdType clientId,SessionIdType sessionId,bool abandon)138     void stop(ClientIdType clientId, SessionIdType sessionId, bool abandon) override {
139         append(abandon ? Abandon(clientId, sessionId) : Stop(clientId, sessionId));
140     }
141 
onFinished(ClientIdType clientId,SessionIdType sessionId)142     void onFinished(ClientIdType clientId, SessionIdType sessionId) {
143         append(Finished(clientId, sessionId));
144     }
145 
onFailed(ClientIdType clientId,SessionIdType sessionId,TranscodingErrorCode err)146     void onFailed(ClientIdType clientId, SessionIdType sessionId, TranscodingErrorCode err) {
147         append(Failed(clientId, sessionId), err);
148     }
149 
onCreated()150     void onCreated() {
151         std::scoped_lock lock{mLock};
152         mGeneration++;
153     }
154 
155     struct Event {
156         enum { NoEvent, Start, Pause, Resume, Stop, Finished, Failed, Abandon } type;
157         ClientIdType clientId;
158         SessionIdType sessionId;
159     };
160 
161     static constexpr Event NoEvent = {Event::NoEvent, 0, 0};
162 
163 #define DECLARE_EVENT(action)                                             \
164     static Event action(ClientIdType clientId, SessionIdType sessionId) { \
165         return {Event::action, clientId, sessionId};                      \
166     }
167 
168     DECLARE_EVENT(Start);
169     DECLARE_EVENT(Pause);
170     DECLARE_EVENT(Resume);
171     DECLARE_EVENT(Stop);
172     DECLARE_EVENT(Finished);
173     DECLARE_EVENT(Failed);
174     DECLARE_EVENT(Abandon);
175 
176     // Push 1 event to back.
append(const Event & event,const TranscodingErrorCode err=TranscodingErrorCode::kNoError)177     void append(const Event& event,
178                 const TranscodingErrorCode err = TranscodingErrorCode::kNoError) {
179         std::unique_lock lock(mLock);
180 
181         mEventQueue.push_back(event);
182         // Error is sticky, non-error event will not erase it, only getLastError()
183         // clears last error.
184         if (err != TranscodingErrorCode::kNoError) {
185             mLastErrorQueue.push_back(err);
186         }
187         mCondition.notify_one();
188     }
189 
190     // Pop 1 event from front, wait for up to timeoutUs if empty.
popEvent(int64_t timeoutUs=0)191     const Event& popEvent(int64_t timeoutUs = 0) {
192         std::unique_lock lock(mLock);
193 
194         if (mEventQueue.empty() && timeoutUs > 0) {
195             mCondition.wait_for(lock, std::chrono::microseconds(timeoutUs));
196         }
197 
198         if (mEventQueue.empty()) {
199             mPoppedEvent = NoEvent;
200         } else {
201             mPoppedEvent = *mEventQueue.begin();
202             mEventQueue.pop_front();
203         }
204 
205         return mPoppedEvent;
206     }
207 
getLastError()208     TranscodingErrorCode getLastError() {
209         std::scoped_lock lock{mLock};
210         if (mLastErrorQueue.empty()) {
211             return TranscodingErrorCode::kNoError;
212         }
213         TranscodingErrorCode err = mLastErrorQueue.front();
214         mLastErrorQueue.pop_front();
215         return err;
216     }
217 
getGeneration()218     int32_t getGeneration() {
219         std::scoped_lock lock{mLock};
220         return mGeneration;
221     }
222 
223 private:
224     std::mutex mLock;
225     std::condition_variable mCondition;
226     Event mPoppedEvent;
227     std::list<Event> mEventQueue;
228     std::list<TranscodingErrorCode> mLastErrorQueue;
229     int32_t mGeneration;
230 };
231 
operator ==(const TestTranscoder::Event & lhs,const TestTranscoder::Event & rhs)232 bool operator==(const TestTranscoder::Event& lhs, const TestTranscoder::Event& rhs) {
233     return lhs.type == rhs.type && lhs.clientId == rhs.clientId && lhs.sessionId == rhs.sessionId;
234 }
235 
236 struct TestClientCallback : public BnTranscodingClientCallback {
TestClientCallbackandroid::TestClientCallback237     TestClientCallback(TestTranscoder* owner, ClientIdType clientId, uid_t clientUid)
238           : mOwner(owner), mClientId(clientId), mClientUid(clientUid) {
239         ALOGD("TestClient Created");
240     }
241 
clientIdandroid::TestClientCallback242     ClientIdType clientId() const { return mClientId; }
clientUidandroid::TestClientCallback243     uid_t clientUid() const { return mClientUid; }
244 
openFileDescriptorandroid::TestClientCallback245     Status openFileDescriptor(const std::string& /*in_fileUri*/, const std::string& /*in_mode*/,
246                               ::ndk::ScopedFileDescriptor* /*_aidl_return*/) override {
247         return Status::ok();
248     }
249 
onTranscodingStartedandroid::TestClientCallback250     Status onTranscodingStarted(int32_t /*in_sessionId*/) override { return Status::ok(); }
251 
onTranscodingPausedandroid::TestClientCallback252     Status onTranscodingPaused(int32_t /*in_sessionId*/) override { return Status::ok(); }
253 
onTranscodingResumedandroid::TestClientCallback254     Status onTranscodingResumed(int32_t /*in_sessionId*/) override { return Status::ok(); }
255 
onTranscodingFinishedandroid::TestClientCallback256     Status onTranscodingFinished(int32_t in_sessionId,
257                                  const TranscodingResultParcel& in_result) override {
258         EXPECT_EQ(in_sessionId, in_result.sessionId);
259         ALOGD("TestClientCallback: received onTranscodingFinished");
260         mOwner->onFinished(mClientId, in_sessionId);
261         return Status::ok();
262     }
263 
onTranscodingFailedandroid::TestClientCallback264     Status onTranscodingFailed(int32_t in_sessionId, TranscodingErrorCode in_errorCode) override {
265         mOwner->onFailed(mClientId, in_sessionId, in_errorCode);
266         return Status::ok();
267     }
268 
onAwaitNumberOfSessionsChangedandroid::TestClientCallback269     Status onAwaitNumberOfSessionsChanged(int32_t /* in_sessionId */,
270                                           int32_t /* in_oldAwaitNumber */,
271                                           int32_t /* in_newAwaitNumber */) override {
272         return Status::ok();
273     }
274 
onProgressUpdateandroid::TestClientCallback275     Status onProgressUpdate(int32_t /* in_sessionId */, int32_t /* in_progress */) override {
276         return Status::ok();
277     }
278 
~TestClientCallbackandroid::TestClientCallback279     virtual ~TestClientCallback() { ALOGI("TestClient destroyed"); };
280 
281 private:
282     TestTranscoder* mOwner;
283     ClientIdType mClientId;
284     uid_t mClientUid;
285     TestClientCallback(const TestClientCallback&) = delete;
286     TestClientCallback& operator=(const TestClientCallback&) = delete;
287 };
288 
289 class TranscodingSessionControllerTest : public ::testing::Test {
290 public:
TranscodingSessionControllerTest()291     TranscodingSessionControllerTest() { ALOGI("TranscodingSessionControllerTest created"); }
~TranscodingSessionControllerTest()292     ~TranscodingSessionControllerTest() { ALOGD("TranscodingSessionControllerTest destroyed"); }
293 
SetUp()294     void SetUp() override {
295         ALOGI("TranscodingSessionControllerTest set up");
296         mTranscoder.reset(new TestTranscoder());
297         mUidPolicy.reset(new TestUidPolicy());
298         mResourcePolicy.reset(new TestResourcePolicy());
299         mThermalPolicy.reset(new TestThermalPolicy());
300         // Overrid default burst params with shorter values for testing.
301         TranscodingSessionController::ControllerConfig config = {
302                 .pacerBurstThresholdMs = 500,
303                 .pacerBurstCountQuota = 10,
304                 .pacerBurstTimeQuotaSeconds = 3,
305         };
306         mController.reset(new TranscodingSessionController(
307                 [this](const std::shared_ptr<TranscoderCallbackInterface>& /*cb*/) {
308                     // Here we require that the SessionController clears out all its refcounts of
309                     // the transcoder object when it calls create.
310                     EXPECT_EQ(mTranscoder.use_count(), 1);
311                     mTranscoder->onCreated();
312                     return mTranscoder;
313                 },
314                 mUidPolicy, mResourcePolicy, mThermalPolicy, &config));
315         mUidPolicy->setCallback(mController);
316 
317         // Set priority only, ignore other fields for now.
318         mOfflineRequest.priority = TranscodingSessionPriority::kUnspecified;
319         mRealtimeRequest.priority = TranscodingSessionPriority::kHigh;
320         mClientCallback0 = ::ndk::SharedRefBase::make<TestClientCallback>(mTranscoder.get(),
321                                                                           CLIENT(0), UID(0));
322         mClientCallback1 = ::ndk::SharedRefBase::make<TestClientCallback>(mTranscoder.get(),
323                                                                           CLIENT(1), UID(1));
324         mClientCallback2 = ::ndk::SharedRefBase::make<TestClientCallback>(mTranscoder.get(),
325                                                                           CLIENT(2), UID(2));
326         mClientCallback3 = ::ndk::SharedRefBase::make<TestClientCallback>(mTranscoder.get(),
327                                                                           CLIENT(3), UID(3));
328     }
329 
TearDown()330     void TearDown() override { ALOGI("TranscodingSessionControllerTest tear down"); }
331 
expectTimeout(int64_t clientId,int32_t sessionId,int32_t generation)332     void expectTimeout(int64_t clientId, int32_t sessionId, int32_t generation) {
333         EXPECT_EQ(mTranscoder->popEvent(2900000), TestTranscoder::NoEvent);
334         EXPECT_EQ(mTranscoder->popEvent(200000), TestTranscoder::Abandon(clientId, sessionId));
335         EXPECT_EQ(mTranscoder->popEvent(100000), TestTranscoder::Failed(clientId, sessionId));
336         EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kWatchdogTimeout);
337         // Should have created new transcoder.
338         EXPECT_EQ(mTranscoder->getGeneration(), generation);
339         EXPECT_EQ(mTranscoder.use_count(), 2);
340         // b/240537336: Allow extra time to finish onError call
341         sleep(1);
342     }
343 
testPacerHelper(int numSubmits,int sessionDurationMs,int expectedSuccess)344     void testPacerHelper(int numSubmits, int sessionDurationMs, int expectedSuccess) {
345         testPacerHelper(numSubmits, sessionDurationMs, expectedSuccess, mClientCallback0, {},
346                         false /*pauseLastSuccessSession*/, true /*useRealCallingUid*/);
347     }
348 
testPacerHelperWithPause(int numSubmits,int sessionDurationMs,int expectedSuccess)349     void testPacerHelperWithPause(int numSubmits, int sessionDurationMs, int expectedSuccess) {
350         testPacerHelper(numSubmits, sessionDurationMs, expectedSuccess, mClientCallback0, {},
351                         true /*pauseLastSuccessSession*/, true /*useRealCallingUid*/);
352     }
353 
testPacerHelperWithMultipleUids(int numSubmits,int sessionDurationMs,int expectedSuccess,const std::shared_ptr<TestClientCallback> & client,const std::vector<int> & additionalClientUids)354     void testPacerHelperWithMultipleUids(int numSubmits, int sessionDurationMs, int expectedSuccess,
355                                          const std::shared_ptr<TestClientCallback>& client,
356                                          const std::vector<int>& additionalClientUids) {
357         testPacerHelper(numSubmits, sessionDurationMs, expectedSuccess, client,
358                         additionalClientUids, false /*pauseLastSuccessSession*/,
359                         true /*useRealCallingUid*/);
360     }
361 
testPacerHelperWithSelfUid(int numSubmits,int sessionDurationMs,int expectedSuccess)362     void testPacerHelperWithSelfUid(int numSubmits, int sessionDurationMs, int expectedSuccess) {
363         testPacerHelper(numSubmits, sessionDurationMs, expectedSuccess, mClientCallback0, {},
364                         false /*pauseLastSuccessSession*/, false /*useRealCallingUid*/);
365     }
366 
testPacerHelper(int numSubmits,int sessionDurationMs,int expectedSuccess,const std::shared_ptr<TestClientCallback> & client,const std::vector<int> & additionalClientUids,bool pauseLastSuccessSession,bool useRealCallingUid)367     void testPacerHelper(int numSubmits, int sessionDurationMs, int expectedSuccess,
368                          const std::shared_ptr<TestClientCallback>& client,
369                          const std::vector<int>& additionalClientUids, bool pauseLastSuccessSession,
370                          bool useRealCallingUid) {
371         uid_t callingUid = useRealCallingUid ? ::getuid() : client->clientUid();
372         for (int i = 0; i < numSubmits; i++) {
373             mController->submit(client->clientId(), SESSION(i), callingUid, client->clientUid(),
374                                 mRealtimeRequest, client);
375             for (int additionalUid : additionalClientUids) {
376                 mController->addClientUid(client->clientId(), SESSION(i), additionalUid);
377             }
378         }
379         for (int i = 0; i < expectedSuccess; i++) {
380             EXPECT_EQ(mTranscoder->popEvent(),
381                       TestTranscoder::Start(client->clientId(), SESSION(i)));
382             if ((i == expectedSuccess - 1) && pauseLastSuccessSession) {
383                 // Insert a pause of 3 sec to the last success running session
384                 mController->onThrottlingStarted();
385                 EXPECT_EQ(mTranscoder->popEvent(),
386                           TestTranscoder::Pause(client->clientId(), SESSION(i)));
387                 sleep(3);
388                 mController->onThrottlingStopped();
389                 EXPECT_EQ(mTranscoder->popEvent(),
390                           TestTranscoder::Resume(client->clientId(), SESSION(i)));
391             }
392             usleep(sessionDurationMs * 1000);
393             // Test half of Finish and half of Error, both should be counted as burst runs.
394             if (i & 1) {
395                 mController->onFinish(client->clientId(), SESSION(i));
396                 EXPECT_EQ(mTranscoder->popEvent(),
397                           TestTranscoder::Finished(client->clientId(), SESSION(i)));
398             } else {
399                 mController->onError(client->clientId(), SESSION(i),
400                                      TranscodingErrorCode::kUnknown);
401                 EXPECT_EQ(mTranscoder->popEvent(100000),
402                           TestTranscoder::Failed(client->clientId(), SESSION(i)));
403                 EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kUnknown);
404             }
405         }
406         for (int i = expectedSuccess; i < numSubmits; i++) {
407             EXPECT_EQ(mTranscoder->popEvent(),
408                       TestTranscoder::Failed(client->clientId(), SESSION(i)));
409             EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kDroppedByService);
410         }
411     }
412 
413     std::shared_ptr<TestTranscoder> mTranscoder;
414     std::shared_ptr<TestUidPolicy> mUidPolicy;
415     std::shared_ptr<TestResourcePolicy> mResourcePolicy;
416     std::shared_ptr<TestThermalPolicy> mThermalPolicy;
417     std::shared_ptr<TranscodingSessionController> mController;
418     TranscodingRequestParcel mOfflineRequest;
419     TranscodingRequestParcel mRealtimeRequest;
420     std::shared_ptr<TestClientCallback> mClientCallback0;
421     std::shared_ptr<TestClientCallback> mClientCallback1;
422     std::shared_ptr<TestClientCallback> mClientCallback2;
423     std::shared_ptr<TestClientCallback> mClientCallback3;
424 };
425 
TEST_F(TranscodingSessionControllerTest,TestSubmitSession)426 TEST_F(TranscodingSessionControllerTest, TestSubmitSession) {
427     ALOGD("TestSubmitSession");
428 
429     // Start with UID(1) on top.
430     mUidPolicy->setTop(UID(1));
431 
432     // Submit offline session to CLIENT(0) in UID(0).
433     // Should start immediately (because this is the only session).
434     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mOfflineRequest, mClientCallback0);
435     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), 0));
436 
437     // Submit real-time session to CLIENT(0).
438     // Should pause offline session and start new session,  even if UID(0) is not on top.
439     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
440     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
441     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
442 
443     // Submit real-time session to CLIENT(0), should be queued after the previous session.
444     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
445     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
446 
447     // Submit real-time session to CLIENT(1) in same uid, should be queued after the previous
448     // session.
449     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(0), mRealtimeRequest, mClientCallback1);
450     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
451 
452     // Submit real-time session to CLIENT(2) in UID(1).
453     // Should pause previous session and start new session, because UID(1) is (has been) top.
454     mController->submit(CLIENT(2), SESSION(0), UID(2), UID(1), mRealtimeRequest, mClientCallback2);
455     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(1)));
456     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(2), SESSION(0)));
457 
458     // Submit offline session, shouldn't generate any event.
459     mController->submit(CLIENT(2), SESSION(1), UID(2), UID(1), mOfflineRequest, mClientCallback2);
460     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
461 
462     // Bring UID(0) to top.
463     mUidPolicy->setTop(UID(0));
464     // Should pause current session, and resume last session in UID(0).
465     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(2), SESSION(0)));
466     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(1)));
467 }
468 
TEST_F(TranscodingSessionControllerTest,TestCancelSession)469 TEST_F(TranscodingSessionControllerTest, TestCancelSession) {
470     ALOGD("TestCancelSession");
471 
472     // Submit real-time session SESSION(0), should start immediately.
473     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
474     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
475 
476     // Submit real-time session SESSION(1), should not start.
477     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
478     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
479 
480     // Submit offline session SESSION(2), should not start.
481     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mOfflineRequest, mClientCallback0);
482     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
483 
484     // Cancel queued real-time session.
485     // Cancel real-time session SESSION(1), should be cancelled.
486     EXPECT_TRUE(mController->cancel(CLIENT(0), SESSION(1)));
487 
488     // Cancel queued offline session.
489     // Cancel offline session SESSION(2), should be cancelled.
490     EXPECT_TRUE(mController->cancel(CLIENT(0), SESSION(2)));
491 
492     // Submit offline session SESSION(3), shouldn't cause any event.
493     mController->submit(CLIENT(0), SESSION(3), UID(0), UID(0), mOfflineRequest, mClientCallback0);
494     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
495 
496     // Cancel running real-time session SESSION(0).
497     // - Should be stopped first then cancelled.
498     // - Should also start offline session SESSION(2) because real-time queue is empty.
499     EXPECT_TRUE(mController->cancel(CLIENT(0), SESSION(0)));
500     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(0)));
501     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(3)));
502 
503     // Submit real-time session SESSION(4), offline SESSION(3) should pause and SESSION(4)
504     // should start.
505     mController->submit(CLIENT(0), SESSION(4), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
506     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(3)));
507     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(4)));
508 
509     // Cancel paused SESSION(3). SESSION(3) should be stopped.
510     EXPECT_TRUE(mController->cancel(CLIENT(0), SESSION(3)));
511     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(3)));
512 }
513 
TEST_F(TranscodingSessionControllerTest,TestCancelSessionWithMultipleUids)514 TEST_F(TranscodingSessionControllerTest, TestCancelSessionWithMultipleUids) {
515     ALOGD("TestCancelSessionWithMultipleUids");
516     std::vector<int32_t> clientUids;
517 
518     // Submit real-time session SESSION(0), should start immediately.
519     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
520     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
521 
522     // Submit real-time session SESSION(1), should not start.
523     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
524     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
525 
526     // Submit offline session SESSION(2), should not start.
527     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mOfflineRequest, mClientCallback0);
528     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
529 
530     // UID(1) moves to top.
531     mUidPolicy->setTop(UID(1));
532 
533     // Add UID(1) to the offline SESSION(2), SESSION(2) should start and SESSION(0) should pause.
534     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(2), UID(1)));
535     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
536     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(2)));
537 
538     // Add UID(1) to SESSION(1) as well.
539     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(1), UID(1)));
540 
541     // Cancel SESSION(2), should be cancelled and SESSION(1) should start.
542     EXPECT_TRUE(mController->cancel(CLIENT(0), SESSION(2)));
543     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(2), &clientUids));
544     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(2)));
545     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
546 
547     // Cancel SESSION(1), should be cancelled and SESSION(0) should resume.
548     EXPECT_TRUE(mController->cancel(CLIENT(0), SESSION(1)));
549     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(1), &clientUids));
550     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(1)));
551     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
552 }
553 
TEST_F(TranscodingSessionControllerTest,TestCancelAllSessionsForClient)554 TEST_F(TranscodingSessionControllerTest, TestCancelAllSessionsForClient) {
555     // Submit real-time session SESSION(0), should start immediately.
556     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
557     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
558 
559     // Submit real-time session SESSION(1), should not start.
560     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
561     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
562 
563     // Submit offline session SESSION(2), should not start.
564     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mOfflineRequest, mClientCallback0);
565     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
566 
567     std::vector<int32_t> clientUids;
568     // Make some more uids blocked on the sessions.
569     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(0), UID(1)));
570     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(1), UID(1)));
571     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(2), UID(1)));
572     EXPECT_TRUE(mController->getClientUids(CLIENT(0), SESSION(0), &clientUids));
573     EXPECT_EQ(clientUids.size(), 2);
574     EXPECT_TRUE(mController->getClientUids(CLIENT(0), SESSION(1), &clientUids));
575     EXPECT_EQ(clientUids.size(), 2);
576     EXPECT_TRUE(mController->getClientUids(CLIENT(0), SESSION(2), &clientUids));
577     EXPECT_EQ(clientUids.size(), 1);
578 
579     // Cancel all sessions for CLIENT(0) with -1.
580     // Expect SESSION(0) and SESSION(1) to be gone.
581     // Expect SESSION(2) still there with empty client uid list (only kept for offline) and start.
582     EXPECT_TRUE(mController->cancel(CLIENT(0), -1));
583     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(0)));
584     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(0), &clientUids));
585     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(1), &clientUids));
586     EXPECT_TRUE(mController->getClientUids(CLIENT(0), SESSION(2), &clientUids));
587     EXPECT_EQ(clientUids.size(), 0);
588     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(2)));
589 }
590 
TEST_F(TranscodingSessionControllerTest,TestFinishSession)591 TEST_F(TranscodingSessionControllerTest, TestFinishSession) {
592     ALOGD("TestFinishSession");
593 
594     // Start with unspecified top UID.
595     // Finish without any sessions submitted, should be ignored.
596     mController->onFinish(CLIENT(0), SESSION(0));
597     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
598 
599     // Submit offline session SESSION(0), should start immediately.
600     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mOfflineRequest, mClientCallback0);
601     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
602 
603     // Submit real-time session SESSION(1), should pause offline session and start immediately.
604     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
605     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
606     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
607 
608     // Submit real-time session SESSION(2), should not start.
609     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
610     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
611 
612     // Finish when the session never started, should be ignored.
613     mController->onFinish(CLIENT(0), SESSION(2));
614     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
615 
616     // UID(1) moves to top.
617     mUidPolicy->setTop(UID(1));
618     // Submit real-time session to CLIENT(1) in UID(1), should pause previous session and start
619     // new session.
620     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(1), mRealtimeRequest, mClientCallback1);
621     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(1)));
622     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(1), SESSION(0)));
623 
624     // Simulate Finish that arrived late, after pause issued by controller.
625     // Should still be propagated to client, but shouldn't trigger any new start.
626     mController->onFinish(CLIENT(0), SESSION(1));
627     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(0), SESSION(1)));
628 
629     // Finish running real-time session, should start next real-time session in queue.
630     mController->onFinish(CLIENT(1), SESSION(0));
631     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(1), SESSION(0)));
632     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(2)));
633 
634     // Finish running real-time session, should resume next session (offline session) in queue.
635     mController->onFinish(CLIENT(0), SESSION(2));
636     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(0), SESSION(2)));
637     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
638 
639     // Finish running offline session.
640     mController->onFinish(CLIENT(0), SESSION(0));
641     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(0), SESSION(0)));
642 
643     // Duplicate finish for last session, should be ignored.
644     mController->onFinish(CLIENT(0), SESSION(0));
645     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
646 }
647 
TEST_F(TranscodingSessionControllerTest,TestFinishSessionWithMultipleUids)648 TEST_F(TranscodingSessionControllerTest, TestFinishSessionWithMultipleUids) {
649     ALOGD("TestFinishSessionWithMultipleUids");
650     std::vector<int32_t> clientUids;
651 
652     // Start with unspecified top uid.
653     // Submit real-time session SESSION(0), should start immediately.
654     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
655     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
656 
657     // Submit real-time session SESSION(1), should not start.
658     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
659     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
660     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(1), UID(1)));
661 
662     // Submit real-time session SESSION(2), should not start.
663     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
664     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
665     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(2), UID(1)));
666     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(2), UID(2)));
667 
668     // UID(1) moves to top.
669     mUidPolicy->setTop(UID(1));
670     // SESSION(0) should pause, SESSION(1) should start.
671     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
672     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
673 
674     // Finish SESSION(1), SESSION(2) (next in line for UID(1)) should start.
675     mController->onFinish(CLIENT(0), SESSION(1));
676     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(0), SESSION(1)));
677     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(2)));
678     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(1), &clientUids));
679 
680     // Finish SESSION(2), SESSION(0) should resume.
681     mController->onFinish(CLIENT(0), SESSION(2));
682     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(0), SESSION(2)));
683     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
684     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(2), &clientUids));
685 }
686 
TEST_F(TranscodingSessionControllerTest,TestFailSession)687 TEST_F(TranscodingSessionControllerTest, TestFailSession) {
688     ALOGD("TestFailSession");
689 
690     // Start with unspecified top UID.
691     // Fail without any sessions submitted, should be ignored.
692     mController->onError(CLIENT(0), SESSION(0), TranscodingErrorCode::kUnknown);
693     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
694 
695     // Submit offline session SESSION(0), should start immediately.
696     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mOfflineRequest, mClientCallback0);
697     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
698 
699     // Submit real-time session SESSION(1), should pause offline session and start immediately.
700     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
701     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
702     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
703 
704     // Submit real-time session SESSION(2), should not start.
705     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
706     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
707 
708     // Fail when the session never started, should be ignored.
709     mController->onError(CLIENT(0), SESSION(2), TranscodingErrorCode::kUnknown);
710     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
711 
712     // UID(1) moves to top.
713     mUidPolicy->setTop(UID(1));
714     // Submit real-time session to CLIENT(1) in UID(1), should pause previous session and start
715     // new session.
716     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(1), mRealtimeRequest, mClientCallback1);
717     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(1)));
718     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(1), SESSION(0)));
719 
720     // Simulate Fail that arrived late, after pause issued by controller.
721     // Should still be propagated to client, but shouldn't trigger any new start.
722     mController->onError(CLIENT(0), SESSION(1), TranscodingErrorCode::kUnknown);
723     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Failed(CLIENT(0), SESSION(1)));
724     EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kUnknown);
725 
726     // Fail running real-time session, should start next real-time session in queue.
727     mController->onError(CLIENT(1), SESSION(0), TranscodingErrorCode::kUnknown);
728     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Failed(CLIENT(1), SESSION(0)));
729     EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kUnknown);
730     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(2)));
731 
732     // Fail running real-time session, should resume next session (offline session) in queue.
733     mController->onError(CLIENT(0), SESSION(2), TranscodingErrorCode::kUnknown);
734     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Failed(CLIENT(0), SESSION(2)));
735     EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kUnknown);
736     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
737 
738     // Fail running offline session, and test error code propagation.
739     mController->onError(CLIENT(0), SESSION(0), TranscodingErrorCode::kInvalidOperation);
740     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Failed(CLIENT(0), SESSION(0)));
741     EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kInvalidOperation);
742 
743     // Duplicate fail for last session, should be ignored.
744     mController->onError(CLIENT(0), SESSION(0), TranscodingErrorCode::kUnknown);
745     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
746 }
747 
TEST_F(TranscodingSessionControllerTest,TestFailSessionWithMultipleUids)748 TEST_F(TranscodingSessionControllerTest, TestFailSessionWithMultipleUids) {
749     ALOGD("TestFailSessionWithMultipleUids");
750     std::vector<int32_t> clientUids;
751 
752     // Start with unspecified top uid.
753     // Submit real-time session SESSION(0), should start immediately.
754     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
755     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
756 
757     // Submit real-time session SESSION(1), should not start.
758     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
759     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
760     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(1), UID(1)));
761 
762     // Submit real-time session SESSION(2), should not start.
763     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
764     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
765 
766     // UID(1) moves to top.
767     mUidPolicy->setTop(UID(1));
768     // SESSION(0) should pause, SESSION(1) should start.
769     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
770     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
771 
772     // Add UID(1) and UID(2) to SESSION(2).
773     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(2), UID(1)));
774     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(2), UID(2)));
775 
776     // Fail SESSION(1), SESSION(2) (next in line for UID(1)) should start.
777     mController->onError(CLIENT(0), SESSION(1), TranscodingErrorCode::kUnknown);
778     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Failed(CLIENT(0), SESSION(1)));
779     EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kUnknown);
780     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(2)));
781     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(1), &clientUids));
782 
783     // Fail SESSION(2), SESSION(0) should resume.
784     mController->onError(CLIENT(0), SESSION(2), TranscodingErrorCode::kInvalidOperation);
785     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Failed(CLIENT(0), SESSION(2)));
786     EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kInvalidOperation);
787     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
788     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(2), &clientUids));
789 }
790 
TEST_F(TranscodingSessionControllerTest,TestTopUidChanged)791 TEST_F(TranscodingSessionControllerTest, TestTopUidChanged) {
792     ALOGD("TestTopUidChanged");
793 
794     // Start with unspecified top UID.
795     // Submit real-time session to CLIENT(0), session should start immediately.
796     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
797     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
798 
799     // Submit offline session to CLIENT(0), should not start.
800     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(0), mOfflineRequest, mClientCallback1);
801     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
802 
803     // Move UID(1) to top.
804     mUidPolicy->setTop(UID(1));
805     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
806 
807     // Submit real-time session to CLIENT(2) in different uid UID(1).
808     // Should pause previous session and start new session.
809     mController->submit(CLIENT(2), SESSION(0), UID(2), UID(1), mRealtimeRequest, mClientCallback2);
810     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
811     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(2), SESSION(0)));
812 
813     // Bring UID(0) back to top.
814     mUidPolicy->setTop(UID(0));
815     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(2), SESSION(0)));
816     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
817 
818     // Bring invalid uid to top.
819     mUidPolicy->setTop(kInvalidUid);
820     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
821 
822     // Finish session, next real-time session should resume.
823     mController->onFinish(CLIENT(0), SESSION(0));
824     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(0), SESSION(0)));
825     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
826 
827     // Finish session, offline session should start.
828     mController->onFinish(CLIENT(2), SESSION(0));
829     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(2), SESSION(0)));
830     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(1), SESSION(0)));
831 }
832 
TEST_F(TranscodingSessionControllerTest,TestTopUidChangedMultipleUids)833 TEST_F(TranscodingSessionControllerTest, TestTopUidChangedMultipleUids) {
834     ALOGD("TestTopUidChangedMultipleUids");
835 
836     // Start with unspecified top UID.
837     // Submit real-time session to CLIENT(0), session should start immediately.
838     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
839     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
840 
841     // Submit offline session to CLIENT(0), should not start.
842     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(0), mOfflineRequest, mClientCallback1);
843     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
844 
845     // Bring UID(1) to top.
846     mUidPolicy->setTop(UID(1));
847     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
848 
849     // Add UID(1) to SESSION(0), SESSION(0) should continue to run
850     // (no pause&resume of the same session).
851     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(0), UID(1)));
852     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
853 
854     // Bring UID(0) back to top, SESSION(0) should continue to run
855     // (no pause&resume of the same session).
856     mUidPolicy->setTop(UID(0));
857     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
858 
859     // Bring UID(2) to top.
860     mUidPolicy->setTop(UID(2));
861     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
862     // Add UID(2) to the offline session, it should be started.
863     EXPECT_TRUE(mController->addClientUid(CLIENT(1), SESSION(0), UID(2)));
864     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
865     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(1), SESSION(0)));
866 
867     // ADD UID(3) to SESSION(0).
868     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(0), UID(3)));
869     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
870     // Bring UID(3) to top, SESSION(0) should resume.
871     mUidPolicy->setTop(UID(3));
872     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(1), SESSION(0)));
873     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
874 
875     // Now make UID(2) also blocked on CLIENT(0), SESSION(0).
876     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(0), UID(2)));
877 
878     // Bring UID(2) back to top, CLIENT(0), SESSION(0) should continue to run (even if it's
879     // added to UID(2)'s queue later than CLIENT(1)'s SESSION(0)).
880     mUidPolicy->setTop(UID(2));
881     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
882 }
883 
TEST_F(TranscodingSessionControllerTest,TestTopUidSetChanged)884 TEST_F(TranscodingSessionControllerTest, TestTopUidSetChanged) {
885     ALOGD("TestTopUidSetChanged");
886 
887     // Start with unspecified top UID.
888     // Submit real-time session to CLIENT(0), session should start immediately.
889     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
890     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
891 
892     // Submit offline session to CLIENT(0), should not start.
893     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(0), mOfflineRequest, mClientCallback1);
894     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
895 
896     // Set UID(0), UID(1) to top set.
897     // UID(0) should continue to run.
898     mUidPolicy->setTop({UID(0), UID(1)});
899     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
900 
901     // Submit real-time session to CLIENT(2) in different uid UID(1).
902     // UID(0) should pause and UID(1) should start.
903     mController->submit(CLIENT(2), SESSION(0), UID(2), UID(1), mRealtimeRequest, mClientCallback2);
904     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
905     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(2), SESSION(0)));
906 
907     // Remove UID(0) from top set, and only leave UID(1) in the set.
908     // UID(1) should continue to run.
909     mUidPolicy->setTop(UID(1));
910     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
911 
912     // Set UID(0), UID(2) to top set.
913     // UID(1) should continue to run.
914     mUidPolicy->setTop({UID(1), UID(2)});
915     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
916 
917     // Bring UID(0) back to top.
918     mUidPolicy->setTop(UID(0));
919     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(2), SESSION(0)));
920     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
921 
922     // Bring invalid uid to top.
923     mUidPolicy->setTop(kInvalidUid);
924     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
925 
926     // Finish session, next real-time session from UID(1) should resume, even if UID(1)
927     // no longer top.
928     mController->onFinish(CLIENT(0), SESSION(0));
929     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(0), SESSION(0)));
930     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
931 
932     // Finish session, offline session should start.
933     mController->onFinish(CLIENT(2), SESSION(0));
934     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(2), SESSION(0)));
935     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(1), SESSION(0)));
936 }
937 
TEST_F(TranscodingSessionControllerTest,TestUidGone)938 TEST_F(TranscodingSessionControllerTest, TestUidGone) {
939     ALOGD("TestUidGone");
940 
941     mUidPolicy->setTop(UID(0));
942     // Start with unspecified top UID.
943     // Submit real-time sessions to CLIENT(0), session should start immediately.
944     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
945     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
946 
947     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
948     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
949     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(1), UID(1)));
950 
951     // Submit real-time session to CLIENT(1), should not start.
952     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(1), mOfflineRequest, mClientCallback1);
953     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
954     EXPECT_TRUE(mController->addClientUid(CLIENT(1), SESSION(0), UID(1)));
955 
956     // Tell the controller that UID(0) is gone.
957     mUidPolicy->setTop(UID(1));
958     // CLIENT(0)'s SESSION(1) should start, SESSION(0) should be cancelled.
959     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
960     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
961     mController->onUidGone(UID(0));
962     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(0)));
963     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Failed(CLIENT(0), SESSION(0)));
964     EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kUidGoneCancelled);
965 
966     std::vector<int32_t> clientUids;
967     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(0), &clientUids));
968     EXPECT_TRUE(mController->getClientUids(CLIENT(0), SESSION(1), &clientUids));
969     EXPECT_EQ(clientUids.size(), 1);
970     EXPECT_EQ(clientUids[0], UID(1));
971 
972     // Tell the controller that UID(1) is gone too.
973     mController->onUidGone(UID(1));
974     // CLIENT(1)'s SESSION(0) should start, CLIENT(0)'s SESSION(1) should be cancelled.
975     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(1)));
976     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Failed(CLIENT(0), SESSION(1)));
977     EXPECT_EQ(mTranscoder->getLastError(), TranscodingErrorCode::kUidGoneCancelled);
978     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(1), SESSION(0)));
979     // CLIENT(1) SESSION(0) should not have any client uids as it's only kept for offline.
980     EXPECT_TRUE(mController->getClientUids(CLIENT(1), SESSION(0), &clientUids));
981     EXPECT_EQ(clientUids.size(), 0);
982 }
983 
TEST_F(TranscodingSessionControllerTest,TestAddGetClientUids)984 TEST_F(TranscodingSessionControllerTest, TestAddGetClientUids) {
985     ALOGD("TestAddGetClientUids");
986 
987     // Add/get client uids with non-existent session, should fail.
988     std::vector<int32_t> clientUids;
989     uid_t ownUid = ::getuid();
990     EXPECT_FALSE(mController->addClientUid(CLIENT(0), SESSION(0), ownUid));
991     EXPECT_FALSE(mController->getClientUids(CLIENT(0), SESSION(0), &clientUids));
992 
993     // Submit a real-time request.
994     EXPECT_TRUE(mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest,
995                                     mClientCallback0));
996     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
997 
998     // Should have own uid in client uids.
999     EXPECT_TRUE(mController->getClientUids(CLIENT(0), SESSION(0), &clientUids));
1000     EXPECT_EQ(clientUids.size(), 1);
1001     EXPECT_EQ(clientUids[0], UID(0));
1002 
1003     // Add UID(0) again should fail.
1004     EXPECT_FALSE(mController->addClientUid(CLIENT(0), SESSION(0), UID(0)));
1005 
1006     // Add own uid should succeed.
1007     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(0), ownUid));
1008     EXPECT_TRUE(mController->getClientUids(CLIENT(0), SESSION(0), &clientUids));
1009     std::unordered_set<uid_t> uidSet;
1010     uidSet.insert(clientUids.begin(), clientUids.end());
1011     EXPECT_EQ(uidSet.size(), 2);
1012     EXPECT_EQ(uidSet.count(UID(0)), 1);
1013     EXPECT_EQ(uidSet.count(ownUid), 1);
1014 
1015     // Submit an offline request.
1016     EXPECT_TRUE(mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mOfflineRequest,
1017                                     mClientCallback0));
1018     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1019 
1020     // Should not have own uid in client uids.
1021     EXPECT_TRUE(mController->getClientUids(CLIENT(0), SESSION(1), &clientUids));
1022     EXPECT_EQ(clientUids.size(), 0);
1023 
1024     // Move UID(1) to top.
1025     mUidPolicy->setTop(UID(1));
1026     // Add UID(1) to offline session, offline session should start and SESSION(0) should pause.
1027     EXPECT_TRUE(mController->addClientUid(CLIENT(0), SESSION(1), UID(1)));
1028     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
1029     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
1030 }
1031 
1032 /* Test resource lost without thermal throttling */
TEST_F(TranscodingSessionControllerTest,TestResourceLost)1033 TEST_F(TranscodingSessionControllerTest, TestResourceLost) {
1034     ALOGD("TestResourceLost");
1035 
1036     // Start with unspecified top UID.
1037     // Submit real-time session to CLIENT(0), session should start immediately.
1038     mRealtimeRequest.clientPid = PID(0);
1039     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
1040     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
1041 
1042     // Submit offline session to CLIENT(0), should not start.
1043     mOfflineRequest.clientPid = PID(0);
1044     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(0), mOfflineRequest, mClientCallback1);
1045     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1046 
1047     // Move UID(1) to top.
1048     mUidPolicy->setTop(UID(1));
1049     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1050 
1051     // Submit real-time session to CLIENT(2) in different uid UID(1).
1052     // Should pause previous session and start new session.
1053     mRealtimeRequest.clientPid = PID(1);
1054     mController->submit(CLIENT(2), SESSION(0), UID(2), UID(1), mRealtimeRequest, mClientCallback2);
1055     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
1056     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(2), SESSION(0)));
1057 
1058     // Test 0: No call into ResourcePolicy if resource lost is from a non-running
1059     // or non-existent session.
1060     mController->onResourceLost(CLIENT(0), SESSION(0));
1061     EXPECT_EQ(mResourcePolicy->getPid(), kInvalidPid);
1062     mController->onResourceLost(CLIENT(3), SESSION(0));
1063     EXPECT_EQ(mResourcePolicy->getPid(), kInvalidPid);
1064 
1065     // Test 1: No queue change during resource loss.
1066     // Signal resource lost.
1067     mController->onResourceLost(CLIENT(2), SESSION(0));
1068     EXPECT_EQ(mResourcePolicy->getPid(), PID(1));
1069     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1070 
1071     // Signal resource available, CLIENT(2) should resume.
1072     mController->onResourceAvailable();
1073     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
1074 
1075     // Test 2: Change of queue order during resource loss.
1076     // Signal resource lost.
1077     mController->onResourceLost(CLIENT(2), SESSION(0));
1078     EXPECT_EQ(mResourcePolicy->getPid(), PID(1));
1079     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1080 
1081     // Move UID(0) back to top, should have no resume due to no resource.
1082     mUidPolicy->setTop(UID(0));
1083     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1084 
1085     // Signal resource available, CLIENT(0) should resume.
1086     mController->onResourceAvailable();
1087     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
1088 
1089     // Test 3:
1090     mController->onResourceLost(CLIENT(0), SESSION(0));
1091     EXPECT_EQ(mResourcePolicy->getPid(), PID(0));
1092     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1093     // Cancel the paused top session during resource lost.
1094     EXPECT_TRUE(mController->cancel(CLIENT(0), SESSION(0)));
1095     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(0)));
1096     // Signal resource available, CLIENT(2)'s session should start.
1097     mController->onResourceAvailable();
1098     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
1099 
1100     // Test 4: Adding new queue during resource loss.
1101     // Signal resource lost.
1102     mController->onResourceLost(CLIENT(2), SESSION(0));
1103     EXPECT_EQ(mResourcePolicy->getPid(), PID(1));
1104     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1105 
1106     // Move UID(2) to top.
1107     mUidPolicy->setTop(UID(2));
1108 
1109     // Submit real-time session to CLIENT(3) in UID(2), session shouldn't start due to no resource.
1110     mRealtimeRequest.clientPid = PID(2);
1111     mController->submit(CLIENT(3), SESSION(0), UID(3), UID(2), mRealtimeRequest, mClientCallback3);
1112     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1113 
1114     // Signal resource available, CLIENT(3)'s session should start.
1115     mController->onResourceAvailable();
1116     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(3), SESSION(0)));
1117 }
1118 
1119 /* Test thermal throttling without resource lost */
TEST_F(TranscodingSessionControllerTest,TestThermalCallback)1120 TEST_F(TranscodingSessionControllerTest, TestThermalCallback) {
1121     ALOGD("TestThermalCallback");
1122 
1123     // Start with unspecified top UID.
1124     // Submit real-time session to CLIENT(0), session should start immediately.
1125     mRealtimeRequest.clientPid = PID(0);
1126     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
1127     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
1128 
1129     // Submit offline session to CLIENT(0), should not start.
1130     mOfflineRequest.clientPid = PID(0);
1131     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(0), mOfflineRequest, mClientCallback1);
1132     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1133 
1134     // Move UID(1) to top.
1135     mUidPolicy->setTop(UID(1));
1136     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1137 
1138     // Submit real-time session to CLIENT(2) in different uid UID(1).
1139     // Should pause previous session and start new session.
1140     mRealtimeRequest.clientPid = PID(1);
1141     mController->submit(CLIENT(2), SESSION(0), UID(2), UID(1), mRealtimeRequest, mClientCallback2);
1142     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
1143     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(2), SESSION(0)));
1144 
1145     // Test 0: Basic case, no queue change during throttling, top session should pause/resume
1146     // with throttling.
1147     mController->onThrottlingStarted();
1148     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(2), SESSION(0)));
1149     mController->onThrottlingStopped();
1150     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
1151 
1152     // Test 1: Change of queue order during thermal throttling, when throttling stops,
1153     // new top session should resume.
1154     mController->onThrottlingStarted();
1155     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(2), SESSION(0)));
1156     mUidPolicy->setTop(UID(0));
1157     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1158     mController->onThrottlingStopped();
1159     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(0)));
1160 
1161     // Test 2: Cancel session during throttling, when throttling stops, new top
1162     // session should resume.
1163     mController->onThrottlingStarted();
1164     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
1165     // Cancel the paused top session during throttling.
1166     EXPECT_TRUE(mController->cancel(CLIENT(0), SESSION(0)));
1167     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Stop(CLIENT(0), SESSION(0)));
1168     // Throttling stops, CLIENT(2)'s session should start.
1169     mController->onThrottlingStopped();
1170     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
1171 
1172     // Test 3: Add new queue during throttling, when throttling stops, new top
1173     // session should resume.
1174     mController->onThrottlingStarted();
1175     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(2), SESSION(0)));
1176     // Move UID(2) to top.
1177     mUidPolicy->setTop(UID(2));
1178     // Submit real-time session to CLIENT(3) in UID(2), session shouldn't start during throttling.
1179     mRealtimeRequest.clientPid = PID(2);
1180     mController->submit(CLIENT(3), SESSION(0), UID(3), UID(2), mRealtimeRequest, mClientCallback3);
1181     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1182     // Throttling stops, CLIENT(3)'s session should start.
1183     mController->onThrottlingStopped();
1184     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(3), SESSION(0)));
1185 }
1186 
1187 /* Test resource lost and thermal throttling happening simultaneously */
TEST_F(TranscodingSessionControllerTest,TestResourceLostAndThermalCallback)1188 TEST_F(TranscodingSessionControllerTest, TestResourceLostAndThermalCallback) {
1189     ALOGD("TestResourceLostAndThermalCallback");
1190 
1191     // Start with unspecified top UID.
1192     // Submit real-time session to CLIENT(0), session should start immediately.
1193     mRealtimeRequest.clientPid = PID(0);
1194     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
1195     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
1196 
1197     // Submit offline session to CLIENT(0), should not start.
1198     mOfflineRequest.clientPid = PID(0);
1199     mController->submit(CLIENT(1), SESSION(0), UID(1), UID(0), mOfflineRequest, mClientCallback1);
1200     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1201 
1202     // Move UID(1) to top.
1203     mUidPolicy->setTop(UID(1));
1204     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1205 
1206     // Submit real-time session to CLIENT(2) in different uid UID(1).
1207     // Should pause previous session and start new session.
1208     mRealtimeRequest.clientPid = PID(1);
1209     mController->submit(CLIENT(2), SESSION(0), UID(2), UID(1), mRealtimeRequest, mClientCallback2);
1210     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(0)));
1211     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(2), SESSION(0)));
1212 
1213     // Test 0: Resource lost during throttling.
1214     // Throttling starts, top session should pause.
1215     mController->onThrottlingStarted();
1216     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(2), SESSION(0)));
1217     // Signal resource lost, this should get ignored because the session is now paused.
1218     mController->onResourceLost(CLIENT(2), SESSION(0));
1219     EXPECT_EQ(mResourcePolicy->getPid(), kInvalidPid);
1220     // Signal resource available, CLIENT(2) shouldn't resume.
1221     mController->onResourceAvailable();
1222     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1223     // Throttling ends, top session should resume.
1224     mController->onThrottlingStopped();
1225     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
1226 
1227     // Test 1: Throttling during resource lost.
1228     mController->onResourceLost(CLIENT(2), SESSION(0));
1229     EXPECT_EQ(mResourcePolicy->getPid(), PID(1));
1230     mController->onThrottlingStarted();
1231     mController->onThrottlingStopped();
1232     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1233     mController->onResourceAvailable();
1234     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
1235 
1236     // Test 2: Interleaving resource lost and throttling.
1237     mController->onResourceLost(CLIENT(2), SESSION(0));
1238     EXPECT_EQ(mResourcePolicy->getPid(), PID(1));
1239     mController->onThrottlingStarted();
1240     mController->onResourceAvailable();
1241     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::NoEvent);
1242     mController->onThrottlingStopped();
1243     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(2), SESSION(0)));
1244 }
1245 
TEST_F(TranscodingSessionControllerTest,TestTranscoderWatchdogNoHeartbeat)1246 TEST_F(TranscodingSessionControllerTest, TestTranscoderWatchdogNoHeartbeat) {
1247     ALOGD("TestTranscoderWatchdogTimeout");
1248 
1249     // Submit session to CLIENT(0) in UID(0).
1250     // Should start immediately (because this is the only session).
1251     mController->submit(CLIENT(0), SESSION(0), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
1252     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(0)));
1253 
1254     // Test 1: If not sending keep-alive at all, timeout after 3 seconds.
1255     expectTimeout(CLIENT(0), SESSION(0), 2);
1256 }
1257 
TEST_F(TranscodingSessionControllerTest,TestTranscoderWatchdogHeartbeat)1258 TEST_F(TranscodingSessionControllerTest, TestTranscoderWatchdogHeartbeat) {
1259     // Test 2: No timeout as long as keep-alive coming; timeout after keep-alive stops.
1260     mController->submit(CLIENT(0), SESSION(1), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
1261     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(1)));
1262 
1263     for (int i = 0; i < 5; i++) {
1264         EXPECT_EQ(mTranscoder->popEvent(1000000), TestTranscoder::NoEvent);
1265         mController->onHeartBeat(CLIENT(0), SESSION(1));
1266     }
1267     expectTimeout(CLIENT(0), SESSION(1), 2);
1268 }
1269 
TEST_F(TranscodingSessionControllerTest,TestTranscoderWatchdogDuringPause)1270 TEST_F(TranscodingSessionControllerTest, TestTranscoderWatchdogDuringPause) {
1271     int expectedGen = 2;
1272 
1273     // Test 3a: No timeout for paused session even if no keep-alive is sent.
1274     mController->submit(CLIENT(0), SESSION(2), UID(0), UID(0), mOfflineRequest, mClientCallback0);
1275     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(2)));
1276     // Trigger a pause by sending a resource lost.
1277     mController->onResourceLost(CLIENT(0), SESSION(2));
1278     EXPECT_EQ(mTranscoder->popEvent(3100000), TestTranscoder::NoEvent);
1279     mController->onResourceAvailable();
1280     EXPECT_EQ(mTranscoder->popEvent(100000), TestTranscoder::Resume(CLIENT(0), SESSION(2)));
1281     expectTimeout(CLIENT(0), SESSION(2), expectedGen++);
1282 
1283     // Test 3b: No timeout for paused session even if no keep-alive is sent.
1284     mController->submit(CLIENT(0), SESSION(3), UID(0), UID(0), mOfflineRequest, mClientCallback0);
1285     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(3)));
1286     // Let the session run almost to timeout, to test timeout reset after pause.
1287     EXPECT_EQ(mTranscoder->popEvent(2900000), TestTranscoder::NoEvent);
1288     // Trigger a pause by submitting a higher-priority request.
1289     mController->submit(CLIENT(0), SESSION(4), UID(0), UID(0), mRealtimeRequest, mClientCallback0);
1290     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Pause(CLIENT(0), SESSION(3)));
1291     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Start(CLIENT(0), SESSION(4)));
1292     // Finish the higher-priority session, lower-priority session should resume,
1293     // and the timeout should reset to full value.
1294     mController->onFinish(CLIENT(0), SESSION(4));
1295     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Finished(CLIENT(0), SESSION(4)));
1296     EXPECT_EQ(mTranscoder->popEvent(), TestTranscoder::Resume(CLIENT(0), SESSION(3)));
1297     expectTimeout(CLIENT(0), SESSION(3), expectedGen++);
1298 }
1299 
TEST_F(TranscodingSessionControllerTest,TestTranscoderPacerOverCountOnly)1300 TEST_F(TranscodingSessionControllerTest, TestTranscoderPacerOverCountOnly) {
1301     ALOGD("TestTranscoderPacerOverCountOnly");
1302     testPacerHelper(12 /*numSubmits*/, 100 /*sessionDurationMs*/, 12 /*expectedSuccess*/);
1303 }
1304 
TEST_F(TranscodingSessionControllerTest,TestTranscoderPacerOverTimeOnly)1305 TEST_F(TranscodingSessionControllerTest, TestTranscoderPacerOverTimeOnly) {
1306     ALOGD("TestTranscoderPacerOverTimeOnly");
1307     testPacerHelper(5 /*numSubmits*/, 1000 /*sessionDurationMs*/, 5 /*expectedSuccess*/);
1308 }
1309 
TEST_F(TranscodingSessionControllerTest,TestTranscoderPacerOverQuota)1310 TEST_F(TranscodingSessionControllerTest, TestTranscoderPacerOverQuota) {
1311     ALOGD("TestTranscoderPacerOverQuota");
1312     testPacerHelper(12 /*numSubmits*/, 400 /*sessionDurationMs*/, 10 /*expectedSuccess*/);
1313 }
1314 
TEST_F(TranscodingSessionControllerTest,TestTranscoderPacerWithPause)1315 TEST_F(TranscodingSessionControllerTest, TestTranscoderPacerWithPause) {
1316     ALOGD("TestTranscoderPacerDuringPause");
1317     testPacerHelperWithPause(12 /*numSubmits*/, 400 /*sessionDurationMs*/, 10 /*expectedSuccess*/);
1318 }
1319 
1320 /*
1321  * Test the case where multiple client uids request the same session. Session should only
1322  * be dropped when all clients are over quota.
1323  */
TEST_F(TranscodingSessionControllerTest,TestTranscoderPacerMultipleUids)1324 TEST_F(TranscodingSessionControllerTest, TestTranscoderPacerMultipleUids) {
1325     ALOGD("TestTranscoderPacerMultipleUids");
1326     // First, run mClientCallback0 to the point of no quota.
1327     testPacerHelperWithMultipleUids(12 /*numSubmits*/, 400 /*sessionDurationMs*/,
1328                                     10 /*expectedSuccess*/, mClientCallback0, {});
1329     // Make UID(0) block on Client1's sessions too, Client1's quota should not be affected.
1330     testPacerHelperWithMultipleUids(12 /*numSubmits*/, 400 /*sessionDurationMs*/,
1331                                     10 /*expectedSuccess*/, mClientCallback1, {UID(0)});
1332     // Make UID(10) block on Client2's sessions. We expect to see 11 succeeds (instead of 10),
1333     // because the addClientUid() is called after the submit, and first session is already
1334     // started by the time UID(10) is added. UID(10) allowed us to run the 11th session,
1335     // after that both UID(10) and UID(2) are out of quota.
1336     testPacerHelperWithMultipleUids(12 /*numSubmits*/, 400 /*sessionDurationMs*/,
1337                                     11 /*expectedSuccess*/, mClientCallback2, {UID(10)});
1338 }
1339 
1340 /*
1341  * Use same uid for clientUid and callingUid, should not be limited by quota.
1342  */
TEST_F(TranscodingSessionControllerTest,TestTranscoderPacerSelfUid)1343 TEST_F(TranscodingSessionControllerTest, TestTranscoderPacerSelfUid) {
1344     ALOGD("TestTranscoderPacerSelfUid");
1345     testPacerHelperWithSelfUid(12 /*numSubmits*/, 400 /*sessionDurationMs*/,
1346                                12 /*expectedSuccess*/);
1347 }
1348 
1349 }  // namespace android
1350