File size: 3,101 Bytes
5f923cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2026 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "runtime/framework/execution_queue.h"

#include <memory>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/status/status.h"  // from @com_google_absl
#include "absl/synchronization/notification.h"  // from @com_google_absl
#include "runtime/util/test_utils.h"  // IWYU pragma: keep

namespace litert::lm {
namespace {

using ::testing::Eq;
using ::testing::IsFalse;
using ::testing::status::StatusIs;

TEST(ExecutionQueueTest, ExecuteTasksInOrder) {
  ExecutionQueue queue;
  std::vector<int> results;
  absl::Notification done;

  ASSERT_OK(queue.Enqueue([&results] { results.push_back(1); }));
  ASSERT_OK(queue.Enqueue([&results] { results.push_back(2); }));
  ASSERT_OK(queue.Enqueue([&results, &done] {
    results.push_back(3);
    done.Notify();
  }));

  done.WaitForNotification();
  EXPECT_THAT(results, Eq(std::vector<int>{1, 2, 3}));
}

TEST(ExecutionQueueTest, RemoveTask) {
  ExecutionQueue queue;
  absl::Notification task1_start, task1_finish;
  absl::Notification task2_run;
  absl::Notification task3_run;

  // Task 1: Blocks until told to finish.
  ASSERT_OK(queue.Enqueue([&] {
    task1_start.Notify();
    task1_finish.WaitForNotification();
  }));

  // Task 2: Should be cancelled.
  ASSERT_OK_AND_ASSIGN(int id2, queue.Enqueue([&] { task2_run.Notify(); }));

  // Task 3: Runs after task 1 (since task 2 is removed).
  ASSERT_OK(queue.Enqueue([&] { task3_run.Notify(); }));

  // Wait for task 1 to start running.
  task1_start.WaitForNotification();

  // Remove task 2.
  ASSERT_OK(queue.Remove(id2));

  // Allow task 1 to finish.
  task1_finish.Notify();

  // Wait for task 3 to run.
  task3_run.WaitForNotification();

  EXPECT_THAT(task2_run.HasBeenNotified(), IsFalse());
}

TEST(ExecutionQueueTest, RemoveNonExistentTask) {
  ExecutionQueue queue;
  EXPECT_THAT(queue.Remove(23), StatusIs(absl::StatusCode::kNotFound));
}

TEST(ExecutionQueueTest, RemoveRunningTaskFails) {
  // Explicitly manage queue's lifetime via unique_ptr to ensure referenced
  // notifications would not go out of scope.
  auto queue = std::make_unique<ExecutionQueue>();
  absl::Notification task_running, task_continue;

  ASSERT_OK_AND_ASSIGN(int id, queue->Enqueue([&] {
    task_running.Notify();
    task_continue.WaitForNotification();
  }));
  task_running.WaitForNotification();

  EXPECT_THAT(queue->Remove(id), StatusIs(absl::StatusCode::kNotFound));

  task_continue.Notify();
  queue.reset();
}

}  // namespace
}  // namespace litert::lm