zoukankan      html  css  js  c++  java
  • Go异步check简单示例

    异步check代码:

    package main
    
    import (
    	"fmt"
    	"reflect"
    	"time"
    )
    
    type janitor struct {
    	interval time.Duration
    	overtime time.Duration
    }
    
    
    func (j *janitor) Runasyncheck(f interface{}, params ...interface{}) {
    	//fmt.Println(params)
    	//创建周期断续器
    	ticker := time.NewTicker(j.interval)
    	//创建定时器
    	timer := time.NewTimer(j.overtime)
    	loop:
    		for {
    			select {
    			case <-timer.C: //当Timer每次到达设置的时间时就会向管道发送消息,此时超时退出
    				print("超时
    ")
    				break loop
    			case <-ticker.C: //当Ticker每次到达设置的时间时就会向管道发送消息,此时进行异步check操作
    				//print("异步check
    ")
    				fv := reflect.ValueOf(f)
    				realParams := make([]reflect.Value, len(params)) //参数
    				for i, item := range params {
    					realParams[i] = reflect.ValueOf(item)
    				}
    				fv.Call(realParams)
    			}
    		}
    	fmt.Println("Break")
    }

    测试:

    
    
    package main

    import ( "fmt" "time" "testing" ) func hello1() { fmt.Println("123") } func hello2(i string) { fmt.Println(i) } func Test_main(t *testing.T){ j := &janitor{ interval: time.Second, overtime:5*time.Second, } j.Runasyncheck(hello1) j.Runasyncheck(hello2,"23") }

      

  • 相关阅读:
    接口测试总结
    Jmeter教程索引贴
    [转] 配置Log4j
    Jmeter报告优化之New XSL stylesheet
    Jmeter默认报告优化
    iOS 自动移除KVO观察者
    iPhone X 适配 ( iOS 11适配 )
    iOS中自动登录的设计
    iOS APP 安全测试
    APP安全测评checklist---Android
  • 原文地址:https://www.cnblogs.com/-wenli/p/14737981.html
Copyright © 2011-2022 走看看