test-sleep: add a very simple test that prints battery suspend estimates
This commit is contained in:
@@ -150,7 +150,7 @@ static int battery_enumerator_new(sd_device_enumerator **ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) {
|
||||
int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) {
|
||||
void *p;
|
||||
|
||||
assert(capacities_by_name);
|
||||
|
||||
@@ -60,6 +60,7 @@ int can_sleep_state(char **types);
|
||||
int battery_is_low(void);
|
||||
int get_total_suspend_interval(Hashmap *last_capacity, usec_t *ret);
|
||||
int fetch_batteries_capacity_by_name(Hashmap **ret_current_capacity);
|
||||
int get_capacity_by_name(Hashmap *capacities_by_name, const char *name);
|
||||
int estimate_battery_discharge_rate_per_hour(
|
||||
Hashmap *last_capacity,
|
||||
Hashmap *current_capacity,
|
||||
|
||||
@@ -117,6 +117,32 @@ TEST(sleep) {
|
||||
log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r));
|
||||
}
|
||||
|
||||
TEST(fetch_batteries_capacity_by_name) {
|
||||
_cleanup_hashmap_free_ Hashmap *capacity = NULL;
|
||||
int r;
|
||||
|
||||
assert_se(fetch_batteries_capacity_by_name(&capacity) >= 0);
|
||||
log_debug("fetch_batteries_capacity_by_name: %u entries", hashmap_size(capacity));
|
||||
|
||||
const char *name;
|
||||
void *cap;
|
||||
HASHMAP_FOREACH_KEY(cap, name, capacity) {
|
||||
assert(cap); /* Anything non-null is fine. */
|
||||
log_info("Battery %s: capacity = %i", name, get_capacity_by_name(capacity, name));
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
usec_t interval;
|
||||
|
||||
sleep(1);
|
||||
|
||||
r = get_total_suspend_interval(capacity, &interval);
|
||||
assert_se(r >= 0 || r == -ENOENT);
|
||||
log_info("%d: get_total_suspend_interval: %s", i,
|
||||
r < 0 ? STRERROR(r) : FORMAT_TIMESPAN(interval, USEC_PER_SEC));
|
||||
}
|
||||
}
|
||||
|
||||
static int intro(void) {
|
||||
if (getuid() != 0)
|
||||
log_warning("This program is unlikely to work for unprivileged users");
|
||||
|
||||
Reference in New Issue
Block a user