Node.js - Ayuda solo un hilo

 
Vista:

Ayuda solo un hilo

Publicado por Fran (1 intervención) el 06/08/2019 09:10:05
Buenas, estoy intentando hacer un script que lea unos ficheros XML, como solo puedo hacer 20 llamadas graphql como limitacion del servidor, necesito meter un sleep cada 20. El problema que tengo ademas es que node.js es multihilo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const fs = require('fs')
const path = require('path')
const xml2js = require('xml2js');
const parser = new xml2js.Parser({ attrkey: "ATTR" });
const sleep = require('sleep');
 
async function myFunc(){
    const folderPath = 'C:/Users/fgomezb/Documents/mercadona'
    //console.log(fs.readdirSync(folderPath))
 
    var folders = fs.readdirSync(folderPath)
 
    /*for (folder in folders) {
        console.log(folder)
    }*/
 
    var i = 0;
    var x = 0;
    var title, description, project, testplanId;
    var testIds = [];
    var testTitle, testSummary, currentTest;
    counter = 0;
 
    while (i < folders.length) {
        //while (1 < 1) {
        await console.log(folders[i]);
        const folderPathProject = 'C:/Users/fgomezb/Documents/mercadona/' + folders[i]
        const ficherosXML = fs.readdirSync(folderPathProject)
        while (x < ficherosXML.length) {
 
 
            //Obtenemos el tipo de fichero
            const fileToRead = 'C:/Users/fgomezb/Documents/mercadona/' + folders[i] + '/' + ficherosXML[x];
            var tipo = "";
            if (ficherosXML[x].toString().includes("_testscript_")) {
                tipo = "testScript"
            } else if (ficherosXML[x].toString().includes("_testcase_")) {
                tipo = "testCase"
            } else {
                tipo = "testplan"
            }
            //Funcionamos dependiendo
            await async function () {
            switch (tipo) {
                case "testScript":
 
                    break;
                case "testCase":
                    await test(fileToRead);
                    break;
                case "testplan":
                    await testPlan(fileToRead);
                    break;
            }
                if (counter == 20) {
                    await Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100);
                    counter = 0;
                }
                counter++;
            x++;
            }
        }
        x = 0;
        i++;
    }
}
 
 
 
 
async function testPlan(ficheroXML) {
    let xml_string = fs.readFileSync(ficheroXML, "utf8");
 
    xml_string = xml_string.split('ns2:').join('');
    xml_string = xml_string.split('ns3:').join('');
 
    //console.log(xml_string);
 
    parser.parseString(xml_string, function (error, result) {
        if (error === null) {
            //console.log(result)
            title = result.testplan.title[0];
            description = result.testplan.description[0];
            project = result.testplan.projectArea;
 
            //console.log(title)
            //console.log(description)
            //console.log(project[0].ATTR.alias)
        }
        else {
            console.log(error);
        }
    });
 
    const client = require('graphql-client')({
        url: 'https://xray.cloud.xpand-it.com/api/v1/graphql',
        headers: {
            Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnQiOiJiNmE2MTMzMC0wOWZjLTMxZDEtOTVjZC1lYjcxMTE2MjA2OTMiLCJhY2NvdW50SWQiOiI1YzA1NGJhZjU0ZDI4ZjUwODkyNDYyN2MiLCJpYXQiOjE1NjUwNzQ2MjIsImV4cCI6MTU2NTE2MTAyMiwiYXVkIjoiRjBCRTVFMTUwMDBFNDhGMEEyMzk1QUQ1MEMzRDhDQzkiLCJpc3MiOiJjb20ueHBhbmRpdC5wbHVnaW5zLnhyYXkiLCJzdWIiOiJGMEJFNUUxNTAwMEU0OEYwQTIzOTVBRDUwQzNEOENDOSJ9.JWNc3VBifskBpDi-CdUvJi8fS4mrHswCQTufM844um8'
        }
    })
 
    client.query(`
    mutation {
    createTestPlan(
        jira: {
            fields: { summary: "${title}", project: {key: "PJP"}, description: "${description}" }
        }
    ) {
        testPlan {
            issueId
            jira(fields: ["key"])
            }
        warnings
        }
    }
    `, await function (req, res) {
            if (res.status === 401) {
                throw new Error('Not authorized')
            }
        }
    ).then( await function (body) {
        testplanId = body.data.createTestPlan.testPlan.issueId;
        console.log("testPlan ID: " + testplanId)
    })
        .catch(function (err) {
            console.log(err.message + "Ha fallado el testPlan")
        })
}
 
 
async function  test(ficheroXML) {
    let xml_string = fs.readFileSync(ficheroXML, "utf8");
 
    xml_string = xml_string.split('ns2:').join('');
    xml_string = xml_string.split('ns3:').join('');
 
    parser.parseString(xml_string, function (error, result) {
        if (error === null) {
            //console.log(result)
            testTitle = result.testcase.title[0]
            testSummary = result.testcase.description[0]
            //console.log(testTitle);
        }else {
            console.log(error);
        }
    });
 
    const client = require('graphql-client')({
        url: 'https://xray.cloud.xpand-it.com/api/v1/graphql',
        headers: {
            Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnQiOiJiNmE2MTMzMC0wOWZjLTMxZDEtOTVjZC1lYjcxMTE2MjA2OTMiLCJhY2NvdW50SWQiOiI1YzA1NGJhZjU0ZDI4ZjUwODkyNDYyN2MiLCJpYXQiOjE1NjUwNzQ2MjIsImV4cCI6MTU2NTE2MTAyMiwiYXVkIjoiRjBCRTVFMTUwMDBFNDhGMEEyMzk1QUQ1MEMzRDhDQzkiLCJpc3MiOiJjb20ueHBhbmRpdC5wbHVnaW5zLnhyYXkiLCJzdWIiOiJGMEJFNUUxNTAwMEU0OEYwQTIzOTVBRDUwQzNEOENDOSJ9.JWNc3VBifskBpDi-CdUvJi8fS4mrHswCQTufM844um8'
        }
    })
 
    client.query(`
   mutation {
    createTest(
        testType: { name: "Manual" },
        jira: {
            fields: { summary:"${testTitle}", project: {key: "PJP"}, description: """${testSummary}""" }
        }
    ) {
        test {
            issueId
            testType {
                name
            }
            unstructured
            jira(fields: ["key"])
        }
        warnings
    }
}
    `, await function (req, res) {
            if (res.status === 401) {
                throw new Error('Not authorized')
            }
        }
    ).then( await function (body) {
 
 
        currentTest = body.data.createTest.test.issueId;
        console.log("Test id: " + currentTest)
    })
        .catch(function (err) {
            console.log(err.message + " Ha fallado el test")
        })
 
}
 
 
await myFunc();

¿Me podeis hechar una mano?

Muchas gracias.

Un saludo.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder