golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/coordinator/queues_test.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build linux || darwin
     6  
     7  package main
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestHumanDuration(t *testing.T) {
    15  	cases := []struct {
    16  		desc     string
    17  		duration string
    18  		want     string
    19  	}{
    20  		{
    21  			desc:     "format days",
    22  			duration: "99h2m1s",
    23  			want:     "4d3h2m1s",
    24  		},
    25  		{
    26  			desc:     "handle tiny durations",
    27  			duration: "1ns",
    28  			want:     "0s",
    29  		},
    30  		{
    31  			desc:     "handle seconds",
    32  			duration: "3s",
    33  			want:     "3s",
    34  		},
    35  	}
    36  	for _, c := range cases {
    37  		t.Run(c.duration, func(t *testing.T) {
    38  			d, err := time.ParseDuration(c.duration)
    39  			if err != nil {
    40  				t.Fatalf("time.ParseDuration(%q) = %q, %q, wanted no error", c.duration, d, err)
    41  			}
    42  			if got := humanDuration(d); got != c.want {
    43  				t.Errorf("humanDuration(%v) = %q, wanted %q", d, got, c.want)
    44  			}
    45  		})
    46  	}
    47  }