2020-04-05 09:20:50 +03:00
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
2017-01-17 08:58:58 +03:00
package process
import (
2019-11-30 17:40:22 +03:00
"context"
2017-01-17 08:58:58 +03:00
"testing"
2017-11-13 17:51:45 +03:00
"time"
2017-01-17 08:58:58 +03:00
"github.com/stretchr/testify/assert"
)
2021-02-04 00:36:38 +03:00
func TestGetManager ( t * testing . T ) {
go func ( ) {
// test race protection
_ = GetManager ( )
} ( )
pm := GetManager ( )
assert . NotNil ( t , pm )
}
2017-01-17 08:58:58 +03:00
func TestManager_Add ( t * testing . T ) {
2019-11-30 17:40:22 +03:00
pm := Manager { processes : make ( map [ int64 ] * Process ) }
2017-01-17 08:58:58 +03:00
2019-11-30 17:40:22 +03:00
pid := pm . Add ( "foo" , nil )
2017-01-17 08:58:58 +03:00
assert . Equal ( t , int64 ( 1 ) , pid , "expected to get pid 1 got %d" , pid )
2019-11-30 17:40:22 +03:00
pid = pm . Add ( "bar" , nil )
2017-01-17 08:58:58 +03:00
assert . Equal ( t , int64 ( 2 ) , pid , "expected to get pid 2 got %d" , pid )
}
2019-11-30 17:40:22 +03:00
func TestManager_Cancel ( t * testing . T ) {
pm := Manager { processes : make ( map [ int64 ] * Process ) }
ctx , cancel := context . WithCancel ( context . Background ( ) )
pid := pm . Add ( "foo" , cancel )
pm . Cancel ( pid )
select {
case <- ctx . Done ( ) :
default :
assert . Fail ( t , "Cancel should cancel the provided context" )
}
}
2017-01-17 08:58:58 +03:00
func TestManager_Remove ( t * testing . T ) {
2019-11-30 17:40:22 +03:00
pm := Manager { processes : make ( map [ int64 ] * Process ) }
2017-01-17 08:58:58 +03:00
2019-11-30 17:40:22 +03:00
pid1 := pm . Add ( "foo" , nil )
2017-01-17 08:58:58 +03:00
assert . Equal ( t , int64 ( 1 ) , pid1 , "expected to get pid 1 got %d" , pid1 )
2019-11-30 17:40:22 +03:00
pid2 := pm . Add ( "bar" , nil )
2017-01-17 08:58:58 +03:00
assert . Equal ( t , int64 ( 2 ) , pid2 , "expected to get pid 2 got %d" , pid2 )
pm . Remove ( pid2 )
2019-11-30 17:40:22 +03:00
_ , exists := pm . processes [ pid2 ]
2017-01-17 08:58:58 +03:00
assert . False ( t , exists , "PID %d is in the list but shouldn't" , pid2 )
}
2017-11-13 17:51:45 +03:00
func TestExecTimeoutNever ( t * testing . T ) {
// TODO Investigate how to improve the time elapsed per round.
maxLoops := 10
for i := 1 ; i < maxLoops ; i ++ {
_ , stderr , err := GetManager ( ) . ExecTimeout ( 5 * time . Second , "ExecTimeout" , "git" , "--version" )
if err != nil {
t . Fatalf ( "git --version: %v(%s)" , err , stderr )
}
}
}
func TestExecTimeoutAlways ( t * testing . T ) {
maxLoops := 100
for i := 1 ; i < maxLoops ; i ++ {
_ , stderr , err := GetManager ( ) . ExecTimeout ( 100 * time . Microsecond , "ExecTimeout" , "sleep" , "5" )
// TODO Simplify logging and errors to get precise error type. E.g. checking "if err != context.DeadlineExceeded".
if err == nil {
t . Fatalf ( "sleep 5 secs: %v(%s)" , err , stderr )
}
}
}