我是 groovy 的新手。已经使用它几个星期了。我写了一个在 Jenkins 中用作共享库的类。简单的方法和结构工作正常。
但是我打算让它更大,所以我想给它添加单元测试。现在我只想在脚本中导入类,并运行一些断言。但我收到以下错误:
unable to resolve class NonCPS , unable to find class for annotation
@ line 51, column 2.
@NonCPS
Run Code Online (Sandbox Code Playgroud)
Jenkins 需要注释。
图书馆(再次工作)类似于:
class MyTestClass implements Serializable {
MyTestClass() { }
@NonCPS
def testMethod() { }
Run Code Online (Sandbox Code Playgroud)
在我调用的脚本中
import com.blabla.MyTestClass
...
test = new MyTestClass()
Run Code Online (Sandbox Code Playgroud)
如果我只是评论注释以测试它是否有效。我一直在四处寻找,似乎有一些非常酷的项目来测试 Jenkins 管道,包括他们的共享库,但我现在有点困惑,我想在使用更复杂的东西之前先进行这些测试。
从 groovy 文档中,我知道注释应该放在类定义中而不是方法中(这是正确的吗?)但是在 Jenkins 上运行时它是如何工作的?
我在共享库的/ var目录中使用了一些全局方法,并且一切正常。现在我需要保持过程的状态,所以我要编写一个普通的类。基本上,我在'/ src'中有一个叫做'ClassTest.groovy'的类,就像这样;
class ClassTest {
String testString
def method1() { ... }
def method2() { ... }
}
Run Code Online (Sandbox Code Playgroud)
并在管道开始时
library 'testlibrary@'
import ClassTest
Run Code Online (Sandbox Code Playgroud)
结果:
WorkflowScript:2:无法解析类ClassTest @第2行,第1列。import
ClassTest
以前,我只是傻瓜
library 'testlibrary@' _
Run Code Online (Sandbox Code Playgroud)
并使用以下方法
script {
libraryTest.method1()
...
libraryTest.method2()
}
Run Code Online (Sandbox Code Playgroud)
方法在文件“ /var/libraryTest.groovy”中,并且一切正常。所以我知道共享库在那里,但是我对groovy / Jenkins处理类/共享库的方式感到困惑。
导入类的正确方法是什么?我在文档中找不到一个简单的示例(带有Groovy文件,文件结构和管道)。
编辑:我将文件移到src / com / company / ClassTest.groovy并修改为
@Library('testlibrary@') import com.company.ClassTest
def notification = new ClassTest()
Run Code Online (Sandbox Code Playgroud)
但是现在的错误是
意外令牌:程序包@第2行
groovy文件的前两行是:
// src/com/company/ClassTest.groovy
package com.company;
Run Code Online (Sandbox Code Playgroud) 我需要在 windows 和 linux 中计算一个大字符串的哈希值,并且两个操作系统的结果应该是相同的。
对于一个简单的测试代码,我使用 std::hash 为 windows 和 linux 获得了不同的哈希值。这是有道理的,因为每个编译器的 std::hash 的实际实现可能使用不同的算法。
这带来了一个问题:有没有办法使用标准库来实现这一目标?
对我来说更直接的答案是实现我自己的哈希算法,所以它对两个操作系统都是一样的。但这似乎有点矫枉过正。我不想重新发明轮子。
我正在尝试访问fortran代码中的模块变量,从C调用它.我已经调用了一个子例程,但是无法调用变量.
module myModule
use iso_c_binding
implicit none
real(C_FLOAT) aa(3)
contains
subroutine fortranFunction() bind(C)
print *,"hello world from Fortran 90"
aa(1)=1.0;
aa(2)=2.0;
aa(3)=3.0;
end subroutine
end module
Run Code Online (Sandbox Code Playgroud)
而C代码是
#include "stdio.h"
extern void fortranfunction();
extern float mymodule_aa_[3];
int main()
{
printf("hello world from C\n");
fortranfunction();
printf("%f %f %f \n",aa[0],aa[1],aa[2]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在编译
gcc -c ccode.c
gfortran -c fortrancode.f90
gcc fortrancode.o ccode.o -lgfortran -o myprogram
Run Code Online (Sandbox Code Playgroud)
gcc以未定义的引用响应`aa'
我正在使用Grid保存并将二进制数据加载到mongoDB数据库.我正在使用nodejs.按照我能够找到的所有示例(非常相似),我的代码是:
router.get('/getlog',function(req,res) {
if (req.isAuthenticated())
{
var mongo = require('mongodb');
var Grid = require('gridfs-stream');
var db = new mongo.Db('logApp', new mongo.Server("127.0.0.1", 27017));
db.open(function (err) {
if (err) {
return handleError(err);
}
var gfs = Grid(db, mongo);
var readStream = gfs.createReadStream({
_id: req.query.id
}).pipe(res);
});
}
else
res.redirect('/home');
});
Run Code Online (Sandbox Code Playgroud)
req.query.id是我需要的文件的id.我得到的回应是:
MongoError: file with id 557aa98e6f1373cb11e8f294 not opened for writing
Run Code Online (Sandbox Code Playgroud)
这没有任何意义,因为我不写,我正在读文件.
当我使用clang编译一个简单的hello world程序时,在elf64文件的注释中我仍然找到与GCC相关的信息。为什么?我使用的是 clang 而不是 gcc。
我使用的是ubuntu 16.04。
username@ubuntu:~$ clang++ -stdlib=libc++ test.cpp
username@ubuntu:~$ objdump --full-contents --section=.comment a.out
a.out: file format elf64-x86-64
Contents of section .comment:
0000 4743433a 20285562 756e7475 20352e34 GCC: (Ubuntu 5.4
0010 2e302d36 7562756e 7475317e 31362e30 .0-6ubuntu1~16.0
0020 342e3429 20352e34 2e302032 30313630 4.4) 5.4.0 20160
0030 36303900 636c616e 67207665 7273696f 609.clang versio
0040 6e20342e 302e3020 28746167 732f5245 n 4.0.0 (tags/RE
0050 4c454153 455f3430 302f6669 6e616c29 LEASE_400/final)
0060 00 .
username@ubuntu:~$
Run Code Online (Sandbox Code Playgroud)
测试.cpp是:
#include <iostream>
int main(int argc, char* argv[])
{ …Run Code Online (Sandbox Code Playgroud) 我有一个应该每毫秒执行一次的子程序。我想检查一下确实是这样。但是我从不同的函数得到不同的执行时间。我一直试图了解这些函数之间的差异(关于这个主题有几个 SO 问题),但我无法理解我得到的结果。请忘记全局变量等。这是一个遗留代码,用 C 编写,移植到 C++,我正在努力改进,所以很乱。
< header stuff>
std::chrono::high_resolution_clock::time_point tchrono;
int64_t tgettime;
float tclock;
void myfunction(){
<all kinds of calculations>
using ms = std::chrono::duration<double, std::milli>;
std::chrono::high_resolution_clock::time_point tmpchrono = std::chrono::high_resolution_clock::now();
printf("chrono %f (ms): \n",std::chrono::duration_cast<ms>(tmpchrono-tchrono).count());
tchrono = tmpchrono;
struct timeval tv;
gettimeofday (&tv, NULL);
int64_t tmpgettime = (int64_t) tv.tv_sec * 1000000 + tv.tv_usec;
printf("gettimeofday: %lld\n",tmpgettime-tgettime);
tgettime = tmpgettime;
float tmpclock = 1000.0f*((float)clock())/CLOCKS_PER_SEC;
printf("clock %f (ms)\n",tmpclock-tclock);
tclock = tmpclock;
<more stuff>
}
Run Code Online (Sandbox Code Playgroud)
输出是:
chrono 0.998352 (ms):
gettimeofday: 999
clock 0.544922 (ms)
Run Code Online (Sandbox Code Playgroud)
为什么会有差异?我希望时钟至少与其他时钟一样大,或者不是?